How to pass document type parameter to xslt using saxon?

冷暖自知 提交于 2019-12-11 03:41:00

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/24077219/how-to-pass-document-type-parameter-to-xslt-using-saxon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!