对于发送原子数据类型将使用 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因为 XdmValue
是 XdmNode
的基类(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/