select="document('')/*/book:category[@code=current()/@category]/@desc" : select « XSLT stylesheet « XML






select="document('')/*/book:category[@code=current()/@category]/@desc"


File: Data.xml

<?xml version="1.0"?>
<booklist>
  <book category="S">
    <title>title 1</title>
    <author>author 1</author>
  </book>
  <book category="FC">
    <title>author 1</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="1.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="document('')/*/book:category[@code=current()/@category]/@desc" />
          </p>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
  <book:category code="S" desc="Science" />
  <book:category code="CS" desc="Computing" />
  <book:category code="FC" desc="Children's Fiction" />
</xsl:transform>

Output:

<html>
   <body>
      <h1>title 1</h1>
      <p>
                     Category:
                     Science
      </p>
      <h1>author 1</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

1.Parent and attribute
2.select="../@attribute"
3.child
4.Get value from tag with {}
5.Node selection by level
6.Select Node by index
7.Select attribute value and output to a list
8.context position and context size
9.select with if then else
10.select distinct values
11.Select one from the target value list
12.select="@*" (at)
13.Select one tag from a list of tags
14.select="employees/employee[2]/following::contact/name/firstName"
15.select="employees/employee[2]/preceding::contact/name/firstName"
16.select="employee[@dept='programming']"
17.select="employees/head:header/namespace::head"
18.value-of select="person[position()=3]/name"