How to pass document type parameter to xslt using saxon?

To send atomic data types will be used as

transformer.SetParameter(new QName("", "", customXml), new XdmAtomicValue("true"));

How to pass XML / Node as an XSLT parameter from C #?

Could you help me?

after your code it works fine, but I only get the text inside xml (what I pass in the parameter), but not the nodes

XSLT:

  <xsl:param name="look-up" as="document-node()"/>
  <xsl:template match="xpp:document">           
  <w:document xml:space="preserve"> 
        <xsl:value-of select="$look-up"/>
  </w:document>
  </xsl:template>

XML

  <?xml version="1.0" encoding="UTF-8"?>
  <document version="1.0" xmlns="http://www.sdl.com/xpp">
    //some tags 
</document>

passing parameter (xml)

 <Job>
   </id>
 </Job>
0
source share
1 answer

I think you should use an object Processorto build XdmNode, see the documentation that says:

NewDocumentBuilder, , , DocumentBuilder. ( , XdmNode) . XML Stream Uri, DOM, Microsoft XML , XmlNode, XmlReader.

XdmNode SetParameter http://saxonica.com/documentation/html/dotnetdoc/Saxon/Api/XsltTransformer.html#SetParameter%28Saxon.Api.QName,Saxon.Api.XdmValue%29, XdmValue XdmNode (XmlValue β†’ XdmItem β†’ XdmNode).

, Saxon 9.5 HE .NET:

        Processor proc = new Processor();

        DocumentBuilder db = proc.NewDocumentBuilder();

        XsltTransformer trans;
        using (XmlReader xr = XmlReader.Create("../../XSLTFile1.xslt"))
        {
            trans = proc.NewXsltCompiler().Compile(xr).Load();
        }

        XdmNode input, lookup;

        using (XmlReader xr = XmlReader.Create("../../XMLFile1.xml"))
        {
            input = db.Build(xr);
        }

        using (XmlReader xr = XmlReader.Create("../../XMLFile2.xml"))
        {
            lookup = db.Build(xr);
        }

        trans.InitialContextNode = input;

        trans.SetParameter(new QName("lookup-doc"), lookup);

        using (XmlWriter xw = XmlWriter.Create(Console.Out))
        {
            trans.Run(new TextWriterDestination(xw));
        }

XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:param name="lookup-doc"/>

  <xsl:key name="map" match="map" use="key"/>

    <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="item">
    <xsl:copy>
      <xsl:value-of select="key('map', ., $lookup-doc)/value"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

XML

<root>
  <item>foo</item>
</root> 

<root>
  <map>
    <key>foo</key>
    <value>bar</value>
  </map>
</root>

<root>
  <item>bar</item>
</root>
+1

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


All Articles