XML Tutorial/XSLT stylesheet/position

Материал из Web эксперт
Версия от 18:22, 25 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Get current position and text

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


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

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


position() returns a number equal to the context position

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <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>
</data>
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="/">
      <DIV>
        <xsl:for-each select="//BBB">
          <xsl:call-template name="printout"/>
        </xsl:for-each>
      </DIV>
      <DIV>
        <xsl:apply-templates select="//CCC"/>
      </DIV>
      <DIV>
        <xsl:apply-templates select="//AAA[last()]//CCC"/>
      </DIV>
    </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"?><DIV>BBB(1/5)(2/5)(3/5)(4/5)(5/5)</DIV><DIV>CCC(1/5)(2/5)(3/5)(4/5)(5/5)</DIV><DIV>CCC(1/4)(2/4)(3/4)(4/4)</DIV>


position() returns a value equal to the context position

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