XPath expression to select all XML child nodes except a specific list?

前端 未结 2 1886
挽巷
挽巷 2020-12-05 10:50

Here\'s the sample data:


    
        Empire Burlesque
        Bob Dylan
           


        
相关标签:
2条回答
  • 2020-12-05 11:24

    I'd start experimenting with something like

    "//cd/*[(name() != 'price') and (name() != 'year')]"
    

    Or you just do normal recursive template matching with <xsl:apply-templates/>, and then have empty templates for <price/> and <year/> elements:

    <xsl:template match="price"/>
    <xsl:template match="year"/>
    
    0 讨论(0)
  • 2020-12-05 11:41
    <xsl:for-each select="//cd/*[not(self::price or self::year)]">
    

    But actually this is bad and unnecessarily complicated. Better:

    <xsl:template match="catalog">
      <html>
        <body>
          <xsl:apply-templates select="cd/*" />
        </body>
      </html>
    </xsl:template>
    
    <!-- this is an empty template to mute any unwanted elements -->
    <xsl:template match="cd/price | cd/year" />
    
    <!-- this is to output wanted elements -->
    <xsl:template match="cd/*">
      <xsl:text>Current node: </xsl:text>
      <xsl:value-of select="."/>
      <br />
    </xsl:template>
    

    Avoid <xsl:for-each>. Almost all of the time it is the wrong tool and should be substituted by <xsl:apply-templates> and <xsl:template>.

    The above works because of match expression specificity. match="cd/price | cd/year" is more specific than match="cd/*", so it is the preferred template for cd/price or cd/year elements. Don't try to exclude nodes, let them come and handle them by discarding them.

    0 讨论(0)
提交回复
热议问题