【问题标题】:XSL msxsl:node-set problemXSL msxsl:节点集问题
【发布时间】:2009-12-07 13:03:39
【问题描述】:

请帮帮我。我只是想声明一个简单的结果树片段并对其进行迭代。


...

<xsl:variable name="rtf">
  <item-list>
    <item id="1">one</item>
    <item id="2">two</item>
    <item id="3">three</item>
    <item id="4">four</item>
  </item-list>
</xsl:variable>

<xsl:for-each select="msxsl:node-set($rtf)/item-list/item">
  <xsl:value-of select="@id"/>
</xsl:for-each>

...


我完全误解了它的工作原理吗?


编辑: 我正在使用 .NET XslCompiledTransform 并拥有正确的 msxsl 命名空间声明 - xmlns:msxsl="urn:schemas-microsoft-com:xslt"

转换执行得很好 - 问题是没有输出

【问题讨论】:

  • 我认为这本身并没有足够的代码来实际输出任何东西......
  • 我不太确定你的意思,但这是整个 xslt 文件的 sn-p。上面和下面的所有输出都很好。只是 rtf 和节点集的行为不符合预期。

标签: xslt node-set


【解决方案1】:

我怀疑您在样式表中声明了默认命名空间。这将有效地将 元素放入命名空间。要使用 XPath 1.0 选择命名空间限定的元素,您必须始终在表达式中使用前缀。

因此,如果您的样式表顶部有这样的内容:

<xsl:stylesheet xmlns="http://example.com"...>

那么你还需要添加这个:

<xsl:stylesheet xmlns="http://example.com" xmlns:x="http://example.com"...>

然后在 XPath 表达式中使用“x”前缀:

<xsl:for-each select="msxsl:node-set($rtf)/x:item-list/x:item">
  <xsl:value-of select="@id"/>
</xsl:for-each>

让我知道这是否有效。我只是在这里猜测。

【讨论】:

    【解决方案2】:

    与 MSXSL 不同,XslCompiledTransform 提供 node-set() 函数,它是 supposed to be - 在 EXSLT 通用命名空间中:

    <xsl:stylesheet xmlns:exslt="http://exslt.org/common">
      ...
      <xsl:for-each select="exslt:node-set($rtf)/item-list/item">
      ...
    </xsl:stylesheet>
    

    【讨论】:

    • @Pavel Minaev 非常感谢!我使用 xslt 和 python,就我而言,这是一个很好的解决方案。
    【解决方案3】:

    这对我来说看起来不错。

    您是否为扩展函数正确声明了 msxsl 命名空间?像这样的:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
    

    我假设您在这里使用的是 Microsoft XSLT 处理器

    【讨论】:

    • 是的,使用 MS 技术并注册了正确的命名空间。
    猜你喜欢
    • 2015-06-05
    • 2013-06-09
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2011-11-22
    • 2012-01-08
    相关资源
    最近更新 更多