如何使用 saxon 将文档类型参数传递给 xslt? [英] How to pass document type parameter to xslt using saxon?
问题描述
对于发送原子数据类型将使用像
For sending atomic data types will use like
transformer.SetParameter(new QName("", "", customXml), new XdmAtomicValue("true"));
如何将 XML/Node 作为参数从 C# 传递给 XSLT?
how to pass a XML/Node as a param to XSLT from C# ?
你能帮我吗
按照你的代码它工作正常,但我只在xml中获取文本(我在参数中传递的内容)而不是节点
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>
传递参数(xml)
<Job>
</id>
</Job>
推荐答案
我认为您应该使用 Processor
对象来构造一个 XdmNode
,请参阅文档,其中说:
I think you should use the Processor
object to construct an XdmNode
, see the documentation which says:
Processor 提供了一个方法 NewDocumentBuilder,它的名字是意味着,返回一个 DocumentBuilder.这可用于构造一个来自各种来源的文档(特别是 XdmNode).这通过指定 Stream 或 Uri,输入可以来自原始词法 XML,或者它可能来自使用 Microsoft XML 构建的 DOM 文档通过指定 XmlNode 解析器,或者可以提供它通过指定 XmlReader 以编程方式进行.
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 as XdmValue
是 XdmNode
(XmlValue -> XdmItem -> XdmNode) 的基类.
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).
这是一个适用于我的示例,适用于 .NET 上的 Saxon 9.5 HE:
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));
}
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>
这篇关于如何使用 saxon 将文档类型参数传递给 xslt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!