Create id with generate-id() : generate id « XSLT stylesheet « XML






Create id with generate-id()


File: Data.xml
<resorts>
   <resort>
      <name>A</name>
      <details>B</details>
      <hotel>
         <name>C</name>
         <stars>5</stars>
         <address> . . . </address>
      </hotel>
      <hotel>
         <name>L</name>
         <stars>2</stars>
         <address> . . . </address>
      </hotel>
   </resort>
   <resort>
      <name>D</name>
      <details>E</details>
      <hotel>
         <name>W</name>
         <stars>5</stars>
         <address> . . . </address>
      </hotel>
      <hotel>
         <name>P</name>
         <stars>2</stars>
         <address> . . . </address>
      </hotel>
   </resort>
</resorts>


File: Transform.xslt

<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xsl:version="1.0">
  <body>
    <h1>Hotels</h1>
    <xsl:for-each select="//hotel">
      <xsl:sort select="number(stars)" order="descending"
        data-type="number" />
      <h2>
        <xsl:value-of select="name" />
      </h2>
      <p>
        Address:
        <xsl:value-of select="address" />
      </p>
      <p>
        Stars:
        <xsl:value-of select="stars" />
      </p>
      <p>
        Resort:
        <a href="#{generate-id(parent::resort)}">
          <xsl:value-of select="parent::resort/name" />
        </a>
      </p>
    </xsl:for-each>
    <h1>Resorts</h1>
    <xsl:for-each select="//resort">
      <h2>
        <a name="{generate-id()}">
          <xsl:value-of select="name" />
        </a>
      </h2>
      <p>
        <xsl:value-of select="details" />
      </p>
    </xsl:for-each>
  </body>
</html>

Output:

<html>
   <body>
      <h1>Hotels</h1>
      <h2>C</h2>
      <p>
                 Address:
                  . . . 
      </p>
      <p>
                 Stars:
                 5
      </p>
      <p>
                 Resort:
                 <a href="#d3e3">A</a></p>
      <h2>W</h2>
      <p>
                 Address:
                  . . . 
      </p>
      <p>
                 Stars:
                 5
      </p>
      <p>
                 Resort:
                 <a href="#d3e36">D</a></p>
      <h2>L</h2>
      <p>
                 Address:
                  . . . 
      </p>
      <p>
                 Stars:
                 2
      </p>
      <p>
                 Resort:
                 <a href="#d3e3">A</a></p>
      <h2>P</h2>
      <p>
                 Address:
                  . . . 
      </p>
      <p>
                 Stars:
                 2
      </p>
      <p>
                 Resort:
                 <a href="#d3e36">D</a></p>
      <h1>Resorts</h1>
      <h2><a name="d3e3">A</a></h2>
      <p>B</p>
      <h2><a name="d3e36">D</a></h2>
      <p>E</p>
   </body>
</html>

 








Related examples in the same category

1.generate-id() function
2.Get value out of generate-id() with value-of