使用xslt从特定xml元素中排除属性

Ale*_*ros 16 xml xslt

我是xslt的新手.我有以下问题.我需要在xml中删除theAttribute特定元素(例如div)中的特定属性(在示例中).即

<html>
   <head>...</head>
   <body>
      <div id="qaz" theAtribute="44">
      </div>
      <div id ="ddd" theAtribute="4">
         <div id= "ggg" theAtribute="9">
         </div>
      </div>
      <font theAttribute="foo" />
   </body>
</html>
Run Code Online (Sandbox Code Playgroud)

成为

<html>
   <head>...</head>
   <body>
      <div id="qaz">
      </div>
      <div id ="ddd">
         <div id= "ggg">
         </div>
      </div>
      <font theAttribute="foo" />
   </body>
</html>
Run Code Online (Sandbox Code Playgroud)

where属性theAtribute已被删除.我找到了这个, http://www.biglist.com/lists/xsl-list/archives/200404/msg00668.html,我试图找到合适的解决方案.

<xsl:template match="@theAtribute" />

从整个文件中删除它...和其他像匹配,如果选择等等没有什么工作.. :-(你能帮我这个吗?这对我来说听起来微不足道,但是对于xslt,我根本无法应付...

谢谢大家

Mad*_*sen 35

什么不起作用?你想要相同的内容,只是没有@theAtribute

如果是这样,请确保您的样式表具有空模板@theAtribute,但也有一个标识模板,可将其他所有内容复制到输出中:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!--empty template suppresses this attribute-->
    <xsl:template match="@theAtribute" />
    <!--identity template copies everything forward by default-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

如果您只想抑制某些@theAtribute,那么您可以使匹配条件更具体.例如,如果您只想从divwho's中删除该属性@id="qaz",那么您可以使用此模板:

<xsl:template match="@theAtribute[../@id='qaz']" />
Run Code Online (Sandbox Code Playgroud)

或者这个模板:

<xsl:template match="*[@id='qaz']/@theAtribute" />
Run Code Online (Sandbox Code Playgroud)

如果@theAttribute要从所有div元素中删除,请将匹配表达式更改为:

<xsl:template match="div/@theAtribute" />
Run Code Online (Sandbox Code Playgroud)

  • 对于关注者,如果你想排除倍数,它可以是类似`match="@theAtribute | @otherAttribute"` 的东西,它们也可以是完整的 xpath 样式表达式...... (2认同)

lwp*_*ro2 11

在select中,您可以使用name函数排除(或包含)该属性.

例如, <xsl:copy-of select="@*[name(.)!='theAtribute']|node()" />