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

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

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

Get current position and text

   <source lang="xml">

File: Data.xml <colors>

 <color>red</color>
 <color>green</color>
 <color>blue</color>
 <color>yellow</color>

</colors> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 <xsl:template match="colors">
   <xsl:for-each select="color">
     <xsl:value-of select="position()" />
     .
     <xsl:value-of select="." />
     <xsl:text>
           </xsl:text>
   </xsl:for-each>
 </xsl:template>
 

</xsl:stylesheet> Output: 1

     .
     red
           2
     .
     green
           3
     .
     blue
           4
     .
     yellow</source>
   
  

if test="position() = last()"

   <source lang="xml">

File: Data.xml <chapter>

 <title>"title 1" Excerpt</title>
 <para>para1</para>
 <figure>
   <title>title 1</title>
 </figure>
 <sect1>
   <sect2>
     <figure>
       <title>title 2</title>
     </figure>
   </sect2>
 </sect1>

</chapter> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 <xsl:template match="chapter">
   <xsl:for-each select="descendant::figure/title">
     <xsl:if test="position() = 1">
       First figure title in chapter:
       <xsl:value-of select="." />
     </xsl:if>
     <xsl:if test="position() = last()">
       Last figure title in chapter:
       <xsl:value-of select="." />
     </xsl:if>
   </xsl:for-each>
 </xsl:template>
 

</xsl:stylesheet> Output:

       First figure title in chapter:
       title 1
       Last figure title in chapter:
       title 2</source>
   
  

position() returns a number equal to the context position

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

   <AAA>
     <BBB>
       <CCC>A</CCC>
     </BBB>
     <BBB/>
     <BBB/>
   </AAA>
   <AAA>
     <BBB/>
     <BBB>
       <CCC>B</CCC>
       <CCC>C</CCC>
       <CCC>D</CCC>
       <CCC>E</CCC>
     </BBB>
   </AAA>

File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet

     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
       <xsl:for-each select="//BBB">
         <xsl:call-template name="printout"/>
       </xsl:for-each>
       <xsl:apply-templates select="//CCC"/>
       <xsl:apply-templates select="//AAA[last()]//CCC"/>
   </xsl:template>
   <xsl:template match="CCC">
     <xsl:call-template name="printout"/>
   </xsl:template>
   <xsl:template name="printout">
     <xsl:if test="position()=1">
       <xsl:value-of select="name()"/>
     </xsl:if>
     <xsl:text>(</xsl:text>
     <xsl:value-of select="position()"/>
     <xsl:text>/</xsl:text>
     <xsl:value-of select="last()"/>
     <xsl:text>)</xsl:text>
   </xsl:template>

</xsl:stylesheet> Output:

<?xml version="1.0" encoding="UTF-8"?>
BBB(1/5)(2/5)(3/5)(4/5)(5/5)
CCC(1/5)(2/5)(3/5)(4/5)(5/5)
CCC(1/4)(2/4)(3/4)(4/4)
</source>


position() returns a value equal to the context position

   <source lang="xml">

File: Data.xml <chapter>

 <title>"title 1" Excerpt</title>
 <para>para1</para>
 <figure>
   <title>title 1</title>
 </figure>
 <para>para2</para>
 <sect1>
   <figure>
     <title>"He Lights"</title>
   </figure>
   <para>line 1</para>
   <sect2>
     <figure>
       <title>title 2</title>
     </figure>
   </sect2>
 </sect1>

</chapter> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 
 <xsl:template match="chapter">
   <xsl:for-each select="descendant::figure/title">
     <xsl:sort />
     <xsl:if test="position() = 1">
       First figure title in chapter:
       <xsl:value-of select="." />
     </xsl:if>
     <xsl:if test="position() = last()">
       Last figure title in chapter:
       <xsl:value-of select="." />
     </xsl:if>
   </xsl:for-each>
 </xsl:template>
 

</xsl:stylesheet> Output:

       First figure title in chapter:
       "He Lights"
       Last figure title in chapter:
       title 2</source>