如何在 XSLT 中迭代 XML 参数

发布于 2024-10-19 06:41:16 字数 1438 浏览 2 评论 0原文

我有一个通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三岁铭 2024-10-26 06:41:17

您可以将任何 XPath/XSLT 数据类型作为参数传递。如何做到这一点完全取决于 XSLT 处理器的实现。

作为此样式表的证明,任何输入(未使用):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="attachment" select="document('parameter.xml')/root"/>
    <xsl:template match="/">
        <xsl:apply-templates select="$attachment"/>
    </xsl:template>
    <xsl:template match="attachment">
        <a target="_blank" href="{@href}">Link</a>
    </xsl:template>
</xsl:stylesheet>

以及 parameter.xml 资源为:

<root>
    <attachment href="file1"/>
    <attachment href="file2"/>
</root> 

输出:

<a target="_blank" href="file1">Link</a>
<a target="_blank" href="file2">Link</a>

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):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="attachment" select="document('parameter.xml')/root"/>
    <xsl:template match="/">
        <xsl:apply-templates select="$attachment"/>
    </xsl:template>
    <xsl:template match="attachment">
        <a target="_blank" href="{@href}">Link</a>
    </xsl:template>
</xsl:stylesheet>

And parameter.xml resource as:

<root>
    <attachment href="file1"/>
    <attachment href="file2"/>
</root> 

Output:

<a target="_blank" href="file1">Link</a>
<a target="_blank" href="file2">Link</a>
终止放荡 2024-10-26 06:41:17

它应为 ...。没有 $ 符号,因为 attachment 是 XML 元素的名称,而不是变量。


在提供完整的 XSLT 和 XML 后进行编辑。

您的 XML 有几个问题:

  • 所有标记都应该关闭。
  • 您不能将 xmlns 用于任何其他用途 — 命名空间。
  • 属性值必须用双引号括起来

,因此 XML 文件的正确版本应该是(例如):

<root>
  <attachment ptr="file1" />
  <attachment ptr="file2" />
</root>

XSLT 文件也有一些问题:

  • xsl 命名空间应该绑定到确切的 URI <代码>http://www.w3.org/1999/XSL/Transform。
  • 您必须至少有一个模板,以便 XSLT 转换处理您的输入 XML 文档。

例如,正确的版本是:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/root">
    <xsl:for-each select="attachment">  
      <a target="_blank" href="{@ptr}"><xsl:value-of select="@ptr" /></a>  
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

我不确定这是否正是您想要的,但对于上述文档,它会生成以下片段:

<a target="_blank" href="file1">file1</a>
<a target="_blank" href="file2">file2</a>

It should read <xsl:for-each select="attachment">.... There is no $ sign because attachment 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:

  • All tags should be closed.
  • You may not use the xmlns for anything else that it's meant for — namespaces.
  • You must have double quotes around the attribute values

So a correct version of the XML file would be (for instance):

<root>
  <attachment ptr="file1" />
  <attachment ptr="file2" />
</root>

The XSLT file has some issues too:

  • The xsl namespace should be bound to the exact URI http://www.w3.org/1999/XSL/Transform.
  • You must have at least a template so that the XSLT transform processes your input XML document.

A correct version would be, for instance:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/root">
    <xsl:for-each select="attachment">  
      <a target="_blank" href="{@ptr}"><xsl:value-of select="@ptr" /></a>  
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

I'm not sure it is exactly what you want, but for the above document it produces the following fragment:

<a target="_blank" href="file1">file1</a>
<a target="_blank" href="file2">file2</a>
糖粟与秋泊 2024-10-26 06:41:17

您可能希望将包含链接的属性值放入其中,如下所示:

<xsl:value-of select="@YourAttribute"/>

这将为当前 xml 元素选择一个属性。

You would want to put the value of your attribute that has the link in it like so:

<xsl:value-of select="@YourAttribute"/>

This selects an attribute for the current xml element.

分開簡單 2024-10-26 06:41:17

您发布的代码有些不正确。引号在哪里,$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.

风筝有风,海豚有海 2024-10-26 06:41:16

XSLT 参数与 XML 标记名称不同。使用标签传递参数,如此处所述。

正如下面的评论中所述,这个问题与上面链接中提供的问题没有太大不同。

StringWriter sw = new StringWriter(); 
XmlTextWriter w = new XmlTextWriter(sw); 
w.WriteStartElement("root"); 
if (!string.IsNullOrEmpty(sWordFileName)) { 
    w.WriteStartElement("attachment", sWordFileName); 
    w.WriteAttributeString("name", sWordFileName);
    w.WriteEndElement(); 
}
if (!string.IsNullOrEmpty(sPDFFileName)) { 
    w.WriteStartElement("attachment");
    w.WriteAttributeString("name", sPDFFileName);
    w.WriteEndElement();
}
w.WriteEndElement();
w.Close();
XPathDocument doc = new XPathDocument(new StringReader(sw.ToString()));
XPathNavigator nav = doc.CreateNavigator();

XsltArgumentList xslArg = new XsltArgumentList();
xslArg.AddParam("attachment","",nav);

这里将是 XSL 来匹配访问包含标记的参数

<?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:template match="/">
        <xsl:apply-templates select="$attachment"/>
    </xsl:template>
    <xsl:template match="attachment">
        <a target="_blank" href="{@name}">{@name}</a>
    </xsl:template>

</xsl:stylesheet>

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.

StringWriter sw = new StringWriter(); 
XmlTextWriter w = new XmlTextWriter(sw); 
w.WriteStartElement("root"); 
if (!string.IsNullOrEmpty(sWordFileName)) { 
    w.WriteStartElement("attachment", sWordFileName); 
    w.WriteAttributeString("name", sWordFileName);
    w.WriteEndElement(); 
}
if (!string.IsNullOrEmpty(sPDFFileName)) { 
    w.WriteStartElement("attachment");
    w.WriteAttributeString("name", sPDFFileName);
    w.WriteEndElement();
}
w.WriteEndElement();
w.Close();
XPathDocument doc = new XPathDocument(new StringReader(sw.ToString()));
XPathNavigator nav = doc.CreateNavigator();

XsltArgumentList xslArg = new XsltArgumentList();
xslArg.AddParam("attachment","",nav);

Here would be XSL to match per Accessing parameters which contain mark-up:

<?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:template match="/">
        <xsl:apply-templates select="$attachment"/>
    </xsl:template>
    <xsl:template match="attachment">
        <a target="_blank" href="{@name}">{@name}</a>
    </xsl:template>

</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文