XML/XSLT stylesheet/current — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:26, 26 мая 2010

Get value with current()

   <source lang="xml">

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">

<xsl:value-of select="title" />

Category: <xsl:value-of select="$categories/category[@code=current()/@category]/@desc" />

       </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>

title 1

Category: Science

title 2

Category: Children"s Fiction

title 3

Category: Children"s Fiction

title 4

Category: Computing

  </body>

</html>

</source>