如何使用saxon将文档类型参数传递给xslt?

时间:2014-06-06 08:17:03

标签: c# xslt-2.0 saxon

对于发送原子数据类型,将使用类似

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

如何将XML / Node作为参数从C#传递给XSLT?

你能帮我吗

跟着你的代码它工作正常,但我只获取xml中的文本(我在参数中传递的内容)但不是节点

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>

传递参数(xml)

 <Job>
   </id>
 </Job>

1 个答案:

答案 0 :(得分:2)

我认为您应该使用Processor对象构建XdmNode,请参阅文档说明:

  

Processor提供了一个方法NewDocumentBuilder,作为名称   暗示,返回一个DocumentBuilder。这可以用于构建一个   来自各种来源的文档(特别是XdmNode)。该   通过指定Stream或Uri,输入可以来自原始词汇XML,   或者它可能来自使用Microsoft XML构建的DOM文档   解析器通过指定XmlNode,或者可以提供   通过提名XmlReader以编程方式。

然后,您可以将XdmNode传递给SetParameter方法http://saxonica.com/documentation/html/dotnetdoc/Saxon/Api/XsltTransformer.html#SetParameter%28Saxon.Api.QName,Saxon.Api.XdmValue%29,因为XdmValueXdmNode的基类(XmlValue - &gt; XdmItem - &gt; XdmNode)。

这是一个适用于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));
        }

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>