if test="ancestor::chapter" : ancestor « XSLT stylesheet « XML






if test="ancestor::chapter"



File: Data.xml

<book>
  <title>title 1</title>
  <chapter>
    <title>chapter 1</title>
    <para>line 1</para>
    <para>line 2</para>
  </chapter>
  <chapter>
    <title>title 2</title>
    <para>line 3</para>
    <para>line 4</para>
  </chapter>
</book>
File: Transform.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  <xsl:template match="para">
    <xsl:if test="ancestor::appendix">
      <p>
        <font face="arial">
          <xsl:apply-templates />
        </font>
      </p>
    </xsl:if>

    <xsl:if test="ancestor::chapter">
      <p>
        <font face="times">
          <xsl:apply-templates />
        </font>
      </p>
    </xsl:if>

  </xsl:template>

  
</xsl:stylesheet>

Output:


  title 1
  
    chapter 1
    <p><font face="times">line 1</font></p>
    <p><font face="times">line 2</font></p>
  
  
    title 2
    <p><font face="times">line 3</font></p>
    <p><font face="times">line 4</font></p>
  

 








Related examples in the same category

1.Get value: $node/ancestor::node()
2.ancestor::node index