descendant-or-self demo : descendant or self « XSLT stylesheet « XML






descendant-or-self demo



File: Data.xml

<chapter author="jm">
  <para>para1</para>
  <para author="ar">para2</para>
  <sect1 author="bd">
    <para>para3</para>
    <para>line 1</para>
    <sect2 author="jm">
      <para>line 2</para>
    </sect2>
  </sect1>
</chapter>

File: Transform.xslt


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

  <xsl:template match="chapter">
    <chapter>
      Authors
      <xsl:for-each select="descendant-or-self::*/@author">
        <xsl:value-of select="." />
        <xsl:text/>
      </xsl:for-each>
      <xsl:apply-templates />
    </chapter>
  </xsl:template>

</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?><chapter>
      Authors
      jmarbdjm
  para1
  para2
  
    para3
    line 1
    
      line 2
    
  
</chapter>

 








Related examples in the same category

1.select="descendant-or-self::*[@dept='programming']"