match element with certain value : contains « XSLT stylesheet « XML






match element with certain value


File: Data.xml
<story>

  <chapter>
    <title>Chapter 1</title>
    <para>para 1</para>
  </chapter>

  <chapter>
    <title>Chapter 2</title>
    <para>item 1</para>
    <para>item 2</para>
    <sect>
      <title>Chapter 2, Section 1</title>
      <para>item 3</para>
      <para>para 2</para>
    </sect>
  </chapter>

  <chapter>
    <title>Chapter 3</title>
    <para>para A</para>
  </chapter>

</story>

File: Transform.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="text" />

  <xsl:template match="chapter[contains(.,'the')]">
    *** This chapter has "the" in it: ***
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="chapter">
    *** This chapter element not processed by other template: ***
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="title" />
</xsl:stylesheet>

Output:



  
    
    
    *** This para element not processed by other template: ***
    para 1
  

  
    
    
    *** This para element not processed by other template: ***
    item 1
    
    *** This para element not processed by other template: ***
    item 2
    
      
      
    *** This para element not processed by other template: ***
    item 3
      
    *** This para element not processed by other template: ***
    para 2
    
  

  
    
    
    *** This para element not processed by other template: ***
    para A
  

 








Related examples in the same category

1.contains function
2.template match="para[contains(.,'the')]"