How to link / embed XSL transformations in .NET.

Context: .NET Framework 3.5

I understand how I can perform one XML conversion using XSLT, but I have not found any good examples for the XML conversion chain.

Input: - An XML document as an XPathDocument. - File paths to multiple XSL files.

Expected Result: - preferably XPathDocument / IXPathNavigable, representing XML with all applied transformations one after another.

Example script :

input xml: <doc></doc>

xsl-1: .xsl, which adds <one />a doc as a child. xsl-2: .xsl, which adds <two />a doc as a child.

Expected Result

<doc><one /><two /></doc>

Goals

Use only XPathDocument / IXPathNavigable or better nature. Avoid loading the entire document into memory.

+3
3

, - ( ):

XslCompiledTransform xsl1 = new XslCompiledTransform();
xsl1.Load("xsl1.xsl");

XslCompiledTransform xsl2 = new XslCompiledTransform();
xsl1.Load("xsl2.xsl");

using (Stream stream = new MemoryStream())
{
     using (XmlReader xmlReader1 = XmlReader.Create("source.xml"))
     {
          xsl1.Transform(xmlReader1, stream);
     }

     stream1.Position = 0;

     using (XmlReader xmlReader2 = XmlReader.Create(stream))
     {
         xsl2.Transform(xmlReader2, "output.xml");
     }
}

xmlreader, , . MemoryStream, .

xslt.

XSLT- (xsltc.exe)

+2

, , XSLT.

, XML (STX), . , STX SourceForge.

:

XML

0

I. , XSLT 1.0 XSLT 1.0, exslt node -set(), .NET XslCompiledTransform.

XSLT 1.0 ext:node-set() , , msxsl:node-set() ( msxsl, ) MSXML.

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ext="http://exslt.org/common"
    exclude-result-prefixes="ext xsl">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*" name="identity">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
  <xsl:variable name="vrtfPass1">
   <xsl:apply-templates select="node()"/>
  </xsl:variable>

  <xsl:apply-templates mode="pass2"
   select="ext:node-set($vrtfPass1)/node()"/>
 </xsl:template>

 <xsl:template match="/*">
     <xsl:copy>
       <xsl:copy-of select="@*"/>
       <one/>
     <xsl:apply-templates/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="node()|@*" mode="pass2">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*" mode="pass2"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/*/one" mode="pass2" >
     <xsl:call-template name="identity"/>
      <two/>
 </xsl:template>
</xsl:stylesheet>

, XML-:

<doc/>

:

<doc>
   <one/>
   <two/>
</doc>

II. XSLT 1.0 XSLT 2.0 XPath, , () XML .

XSLT 2.1 ( 3.0) , , WD , XSLT 3.0- .

III. XslCompiledTransform XPathDocument - XML . , a XPathDocument , , XslCompiledTransform XML-.

0

Source: https://habr.com/ru/post/1765113/


All Articles