ancestor::node index : ancestor « XSLT stylesheet « XML






ancestor::node index


File: Data.xml

<?xml-stylesheet type="text/xsl" href="Transform.xslt"?>
<orgchart date="28 March 2001">
  <person>
    <name>chart 1</name>
    <title>title1</title>
    <reports>
      <person>
        <name>a</name>
        <title>b</title>
        <reports>
          <person>
            <name>c</name>
            <title>d</title>
          </person>
          <person>
            <name>e</name>
            <title>f</title>
          </person>
        </reports>
      </person>
      <person>
        <name>S</name>
        <title>M</title>
      </person>
      <person>
        <name>S</name>
        <title>I</title>
      </person>
    </reports>
  </person>
</orgchart>
 

File: Transform.xslt

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

  <xsl:param name="title">Management Structure</xsl:param>

  <xsl:output indent="yes" />

  <xsl:template match="/">
    <html>
      <head>
        <title>
          <xsl:value-of select="$title" />
        </title>
      </head>
      <body>
        <h1>
          <xsl:value-of select="$title" />
        </h1>
        <p>
          The following responsibilies were announced on
          <xsl:value-of select="/orgchart/@date" />
          :
        </p>
        <table border="2" cellpadding="5">
          <tr>
            <th>Name</th>
            <th>Role</th>
            <th>Reporting to</th>
          </tr>
          <xsl:for-each select="//person">
            <tr>
              <td>
                <xsl:value-of select="name" />
              </td>
              <td>
                <xsl:value-of select="title" />
              </td>
              <td>
                <xsl:value-of
                  select="ancestor::person[1]/name" />
              </td>
            </tr>
          </xsl:for-each>
        </table>
        <hr />
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

Output:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Management Structure</title>
   </head>
   <body>
      <h1>Management Structure</h1>
      <p>
                   The following responsibilies were announced on
                   28 March 2001
                   :
                 
      </p>
      <table border="2" cellpadding="5">
         <tr>
            <th>Name</th>
            <th>Role</th>
            <th>Reporting to</th>
         </tr>
         <tr>
            <td>chart 1</td>
            <td>title1</td>
            <td></td>
         </tr>
         <tr>
            <td>a</td>
            <td>b</td>
            <td>chart 1</td>
         </tr>
         <tr>
            <td>c</td>
            <td>d</td>
            <td>a</td>
         </tr>
         <tr>
            <td>e</td>
            <td>f</td>
            <td>a</td>
         </tr>
         <tr>
            <td>S</td>
            <td>M</td>
            <td>chart 1</td>
         </tr>
         <tr>
            <td>S</td>
            <td>I</td>
            <td>chart 1</td>
         </tr>
      </table>
      <hr>
   </body>
</html>

 








Related examples in the same category

1.if test="ancestor::chapter"
2.Get value: $node/ancestor::node()