Get value with current() : current « XSLT stylesheet « XML






Get value with current()


File: Data.xml

<?xml version="1.0"?>
<booklist>
  <book category="S">
    <title>title 1</title>
    <author>author 1</author>
  </book>
  <book category="FC">
    <title>title 2</title>
    <author>author 1</author>
  </book>
  <book category="FC">
    <title>title 3</title>
    <author>author 1</author>
  </book>
  <book category="CS">
    <title>title 4</title>
    <author>author 1</author>
    <author>author 2</author>
    <author>author 3</author>
    <author>author 4</author>
  </book>
</booklist>


File: Transform.xslt

<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0" xmlns:book="books.uri" exclude-result-prefixes="book">

  <xsl:template match="/">
    <html>
      <body>
        <xsl:for-each select="//book">
          <h1>
            <xsl:value-of select="title" />
          </h1>
          <p>
            Category:
            <xsl:value-of select="$categories/category[@code=current()/@category]/@desc" />
          </p>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>

  <xsl:variable name="categories">
    <category code="S" desc="Science" />
    <category code="CS" desc="Computing" />
    <category code="FC" desc="Children's Fiction" />
  </xsl:variable>

</xsl:transform>

Output:

<html>
   <body>
      <h1>title 1</h1>
      <p>
                     Category:
                     Science
      </p>
      <h1>title 2</h1>
      <p>
                     Category:
                     Children's Fiction
      </p>
      <h1>title 3</h1>
      <p>
                     Category:
                     Children's Fiction
      </p>
      <h1>title 4</h1>
      <p>
                     Category:
                     Computing
      </p>
   </body>
</html>

 








Related examples in the same category