2012-12-15 2 views
0

Asp.NET에서 XML에 XSLT를 적용 :내가 정의 ActionResult를 사용하여 컨트롤러 액션에서 XML을 반환하고있어 MVC

public class XmlActionResult : ActionResult 
{ 
    /// <summary> 
    /// This class is a custom ActionResult that outputs the content of an XML document to the response stream 
    /// </summary> 

    private readonly XDocument _document; 
    public Formatting Formatting { get; set; } 
    public string MimeType { get; set; } 

    public XmlActionResult(XDocument document) 
    { 
     _document = document; 
     MimeType = "text/xml"; 
     Formatting = Formatting.None; 

    } 

    public override void ExecuteResult(ControllerContext context) 
    { 
     context.HttpContext.Response.Clear(); 
     context.HttpContext.Response.ContentType = MimeType; 

     using(var writer = new XmlTextWriter(context.HttpContext.Response.OutputStream, null) 
      { 
       Formatting = Formatting 

      }) 

     _document.WriteTo(writer); 
    } 
} 

이것은 브라우저에 XML 트리를 출력합니다. XML을 변환하는 XSL 파일이 있는데 XML 출력에 스타일 시트를 적용하려면 어떻게해야합니까?

+3

"ASP.NET MVC"는 단순히 "MVC"로 언급하지 마십시오. 하나는 프레임 워크이고 다른 하나는 언어에 독립적 인 디자인 패턴입니다. 그것은 IE와 같은 것입니다 - "인터넷" –

+0

그건 공정한 지적입니다, 사과! –

+0

서버 또는 클라이언트 브라우저에서 XSLT 변환을 어디에서 적용 하시겠습니까? –

답변

0

Use XslCompiledTransform to apply any XSLT 1.0 stylesheet 또는 XSLT 2.0 스타일 시트를 적용하려면 Saxon 9 또는 XmlPrime과 같은 타사 XSLT 2.0 프로세서를 사용하십시오.

당신은 예를 들어, 그것을 확장 방법 CreateNavigator를 호출하여 XslCompiledTransformTransform 방법의 첫 번째 인수로하여하여 XDocument에 전달할 수 있습니다

XslCompiledTransform proc = new XslCompiledTransform(); 
proc.Load("sheet.xsl"); 
poc.Transform(_document.CreateNavigator(), null, context.HttpContext.Response.OutputStream); 

확장 방법을 사용하려면 using System.Xml.XPath; 지시어가 필요합니다. 변환 결과 (예 : content type text/html 또는 XHTML (content type application/xhtml + xml) 또는 기타 형식)에 따라 브라우저에 보내는 ContentType을 변경하여 변환 결과가 파싱되어 적절하게 렌더링됩니다.

관련 문제