c# - 如何使用撒克逊将文档类型参数传递给 xslt?

标签 c# xslt-2.0 saxon

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

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

如何从 C# 将 XML/Node 作为参数传递给 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>

最佳答案

我认为您应该使用 Processor 对象来构造一个 XdmNode,请参阅文档,内容如下:

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.

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

这是一个适用于 .NET 上的 Saxon 9.5 HE 的示例:

        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>

关于c# - 如何使用撒克逊将文档类型参数传递给 xslt?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24077219/

相关文章:

C# 在 lambda 表达式中使用动态字符串或查询

xml - 使用 XSLT 转换 XSLT 时保留实体引用

正则表达式删除节点开头的模式

java - 如何在 Java 中使用 .sef 文件运行 XSLT 转换

html - XSLT:将分组的 html 元素移动到节级别

c# - WinRT DownloadProgress 回调进度状态

c# - HttpWebRequest.GetResponse() 与 GetResponseAsync() 中的超时行为

javascript - 拥有 2D Sprite 脸 3d 相机

xml - 使用 XSLT 标记匹配正则表达式的文本?

xslt - 可以用 xsd :dayTimeDuration() 指定的最长持续时间