Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass document type parameter to xslt using saxon?

Tags:

c#

xslt-2.0

saxon

For sending atomic data types will use like

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

how to pass a XML/Node as a param to XSLT from C# ?

Can you please help me

followed your code it's working fine but i am getting only text inside the xml(what i am passing in parameter) but not 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>
like image 644
it'sme Avatar asked Oct 17 '25 13:10

it'sme


1 Answers

I think you should use the Processor object to construct an XdmNode, see the documentation which says:

The Processor provides a method NewDocumentBuilder which, as the name implies, returns a DocumentBuilder. This may be used to construct a document (specifically, an XdmNode) from a variety of sources. The input can come from raw lexical XML by specifying a Stream or a Uri, or it may come from a DOM document built using the Microsoft XML parser by specifying an XmlNode, or it may be supplied programmatically by nominating an XmlReader.

Then you can pass in the XdmNode to the SetParameter method http://saxonica.com/documentation/html/dotnetdoc/Saxon/Api/XsltTransformer.html#SetParameter%28Saxon.Api.QName,Saxon.Api.XdmValue%29 as XdmValue is a base class of XdmNode (XmlValue -> XdmItem -> XdmNode).

Here is an example that works for me with Saxon 9.5 HE on .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));
        }

The XSLT is

<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>

the XML documents are

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

and

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

the resulting output is

<root>
  <item>bar</item>
</root>
like image 79
Martin Honnen Avatar answered Oct 20 '25 04:10

Martin Honnen