如何在 XSLT 中迭代 XML 参数
我有一个通过 XSLT 转换的 XML 文件。我通过 C# 将 XML 作为参数传递给 XSLT。该参数的名称是attachment,它包含XML。它的编写如下:
StringWriter sw = new StringWriter();
XmlTextWriter w = new XmlTextWriter(sw);
w.WriteStartElement("root");
if (!string.IsNullOrEmpty(sWordFileName)) {
w.WriteStartElement("mylink", sWordFileName);
w.WriteEndElement();
}
if (!string.IsNullOrEmpty(sPDFFileName)) {
w.WriteStartElement("mylink", sPDFFileName);
w.WriteEndElement();
}
w.Close();
XPathDocument doc = new XPathDocument(new StringReader(sw.ToString()));
XPathNavigator nav = doc.CreateNavigator();
_exportSet[currentExportSet].Format.ParamList["attachment"] = nav.Select("./*");
我的 xml 参数看起来像
<root><attachment xmlns=file1><attachment xmlns=file2></root>
现在在 XSLT 中,我需要迭代此 XML 参数并创建一个链接。
这是我的 XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:my-scripts="urn:my-scripts"
xmlns="factiva.com/fcs/schemas/newsSummaries">
<xsl:param name="attachment"/>
<xsl:for-each select="$attachment">
<a target="_blank" href="#"><xsl:copy-of select="."/></a>
</xsl:for-each>
</xsl:stylesheet>
但它没有创建链接。
I have an XML file that I am transforming via XSLT. I am passing an XML as parameter to the XSLT via C#. The parameter's name is attachment and it contains XML. It is written as follows:
StringWriter sw = new StringWriter();
XmlTextWriter w = new XmlTextWriter(sw);
w.WriteStartElement("root");
if (!string.IsNullOrEmpty(sWordFileName)) {
w.WriteStartElement("mylink", sWordFileName);
w.WriteEndElement();
}
if (!string.IsNullOrEmpty(sPDFFileName)) {
w.WriteStartElement("mylink", sPDFFileName);
w.WriteEndElement();
}
w.Close();
XPathDocument doc = new XPathDocument(new StringReader(sw.ToString()));
XPathNavigator nav = doc.CreateNavigator();
_exportSet[currentExportSet].Format.ParamList["attachment"] = nav.Select("./*");
My xml parameter looks like
<root><attachment xmlns=file1><attachment xmlns=file2></root>
Now in XSLT I need to iterate through this XML param and create a link.
Here is my XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:my-scripts="urn:my-scripts"
xmlns="factiva.com/fcs/schemas/newsSummaries">
<xsl:param name="attachment"/>
<xsl:for-each select="$attachment">
<a target="_blank" href="#"><xsl:copy-of select="."/></a>
</xsl:for-each>
</xsl:stylesheet>
But it doesn't create a link.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将任何 XPath/XSLT 数据类型作为参数传递。如何做到这一点完全取决于 XSLT 处理器的实现。
作为此样式表的证明,任何输入(未使用):
以及
parameter.xml
资源为:输出:
You can pass any XPath/XSLT data type as parameters. How to do that entirely depends on the XSLT processor implementation.
As proof this stylesheet, with any input (not used):
And
parameter.xml
resource as:Output:
它应为
...
。没有$
符号,因为attachment
是 XML 元素的名称,而不是变量。在提供完整的 XSLT 和 XML 后进行编辑。
您的 XML 有几个问题:
xmlns
用于任何其他用途 — 命名空间。,因此 XML 文件的正确版本应该是(例如):
XSLT 文件也有一些问题:
xsl
命名空间应该绑定到确切的 URI <代码>http://www.w3.org/1999/XSL/Transform。例如,正确的版本是:
我不确定这是否正是您想要的,但对于上述文档,它会生成以下片段:
It should read
<xsl:for-each select="attachment">...
. There is no$
sign becauseattachment
is the name of an XML element, not a variable.EDIT after you've given the full XSLT and XML.
There are several problems with your XML:
xmlns
for anything else that it's meant for — namespaces.So a correct version of the XML file would be (for instance):
The XSLT file has some issues too:
xsl
namespace should be bound to the exact URIhttp://www.w3.org/1999/XSL/Transform
.A correct version would be, for instance:
I'm not sure it is exactly what you want, but for the above document it produces the following fragment:
您可能希望将包含链接的属性值放入其中,如下所示:
这将为当前 xml 元素选择一个属性。
You would want to put the value of your attribute that has the link in it like so:
This selects an attribute for the current xml element.
您发布的代码有些不正确。引号在哪里,$attachment 是什么?
您可能忘记提及名称空间,要正确选择,您需要编写
select="//file1:attachment"
或类似的内容。The code you posted is somewhat incorrect. Where are the quotes, what is $attachment?
You probably forgot to mention namespace, to select correctly, you need to write
select="//file1:attachment"
or sth like that.XSLT 参数与 XML 标记名称不同。使用标签传递参数,如此处所述。
正如下面的评论中所述,这个问题与上面链接中提供的问题没有太大不同。
这里将是 XSL 来匹配访问包含标记的参数:
An XSLT parameter is different than an XML tag name. Parameters are passed using the tag as described here.
As stated in the comments below, this problem is not too different from what is provided in the link above.
Here would be XSL to match per Accessing parameters which contain mark-up: