【问题标题】:XSLT 2.0 template matching - strip all attributes which are not in the desired namespaceXSLT 2.0 模板匹配 - 去除所有不在所需命名空间中的属性
【发布时间】:2011-12-17 11:26:12
【问题描述】:

我有一个具有不同命名空间属性的 XML - 它基本上是一种扩展的 XHTML。我想转储所有非 xhtml 命名空间属性。

示例源 XML:

<html>
  <body>
    <p class="test" xy:foo="true">blah</p>
  </body>
</html>

目前,我有以下 XSLT 模板:

<xsl:template match="@*">
    <xsl:choose>
        <xsl:when test='namespace-uri()="http://www.w3.org/1999/xhtml"'><xsl:copy-of select="."/></xsl:when>
        <xsl:otherwise></xsl:otherwise>
    </xsl:choose>
</xsl:template>

所需的输出 XML:

<html>
  <body>
    <p class="test">blah</p>
  </body>
</html>

但它似乎不匹配,因为我得到一个完全没有属性的输出 XML。我感觉namespace-uri() 没有按预期工作。有什么想法吗?

【问题讨论】:

    标签: xml xslt xpath xml-namespaces xml-attribute


    【解决方案1】:

    XHTML 元素上的属性(如您的class 之一)是非命名空间中的属性,而不是 XHTML 命名空间中的属性。所以使用

    <xsl:template match="@*[namespace-uri() != '']"/>
    

    加上身份转换模板。

    【讨论】:

      猜你喜欢
      • 2011-11-08
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      相关资源
      最近更新 更多