代码之家  ›  专栏  ›  技术社区  ›  chernevik

基于元素名称子字符串的XSLT节点选择

  •  1
  • chernevik  · 技术社区  · 16 年前

    <foo_bar>Keep this.
      <foo_who>Keep this, too.
        <fu_bar>Don't want this.</fu_bar>
      </foo_who>
    </foo_bar>
    

    我想从中输出:

    <foo_bar>Keep this.
      <foo_who>Keep this, too.
      </foo_who>
    </foo_bar>
    

    1 回复  |  直到 16 年前
        1
  •  2
  •   Kevin Hakanson    16 年前

    Regular Expression Matching in XSLT 2 .

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="*">
            <xsl:variable name="name" select="local-name()"/>
            <xsl:if test="starts-with($name, 'foo')">
                <xsl:copy>
                    <xsl:apply-templates/>
                </xsl:copy>
            </xsl:if>
        </xsl:template>
    </xsl:stylesheet>
    

    它给出了这个输出,它似乎有一个额外的换行符。

    <foo_bar>Keep this.
      <foo_who>Keep this, too.
    
      </foo_who>
    </foo_bar>