XML Tutorial/XSLT stylesheet/last
Содержание
- 1 if test="not(position()=last())"
- 2 last() function is in for-each element. The context is therefore all selected chapters.
- 3 last() function is in template. The context is therefore a single chapter element.
- 4 last() returns a number equal to the context size from the expression evaluation context
- 5 last()- Returns a value equal to the context size
- 6 You can select the last element of given type
if test="not(position()=last())"
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<cars>
<manufacturer name="Chevrolet">
<car>Cavalier</car>
<car>Corvette</car>
<car>Impala</car>
<car>Malibu</car>
</manufacturer>
<manufacturer name="Ford">
<car>Pinto</car>
<car>Mustang</car>
<car>Taurus</car>
</manufacturer>
<manufacturer name="Volkswagen">
<car>Beetle</car>
<car>Jetta</car>
<car>Passat</car>
<car>Touraeg</car>
</manufacturer>
</cars>
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:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="cars/manufacturer/@name">
<xsl:value-of select="."/>
<xsl:if test="not(position()=last())">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
Chevrolet, Ford, Volkswagen
last() function is in for-each element. The context is therefore all selected chapters.
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<chapter>Chapter A</chapter>
<chapter>Chapter B</chapter>
<chapter>Chapter C</chapter>
<chapter>Chapter D</chapter>
</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="/">
<TABLE>
<xsl:for-each select="//chapter">
<TR>
<TD>
<xsl:value-of select="."/>
<xsl:text>(last = </xsl:text>
<xsl:value-of select="last()"/>
<xsl:text>)</xsl:text>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TD>Chapter A(last = 4)</TD></TR><TR><TD>Chapter B(last = 4)</TD></TR><TR><TD>Chapter C(last = 4)</TD></TR><TR><TD>Chapter D(last = 4)</TD></TR></TABLE>
last() function is in template. The context is therefore a single chapter element.
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<chapter>Chapter A</chapter>
<chapter>Chapter B</chapter>
<chapter>Chapter C</chapter>
<chapter>Chapter D</chapter>
</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="/">
<TABLE>
<xsl:for-each select="//chapter">
<TR>
<TD>
<xsl:apply-templates select="."/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
<xsl:template match="chapter">
<xsl:value-of select="."/>
<xsl:text>(last = </xsl:text>
<xsl:value-of select="last()"/>
<xsl:text>)</xsl:text>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TD>Chapter A(last = 1)</TD></TR><TR><TD>Chapter B(last = 1)</TD></TR><TR><TD>Chapter C(last = 1)</TD></TR><TR><TD>Chapter D(last = 1)</TD></TR></TABLE>
last() returns a number equal to the context size from the expression evaluation context
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>
last()- Returns a value equal to the context size
File: Data.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<africa>
<source>
<title>CIA Factbook</title>
<url>http://www.cia.gov/cia/publications/factbook/</url>
<populations estimate="true" year="2002"/>
<nation> <name>Algeria</name> <capital>Algiers</capital> <population>32277942</population> <cc>dz</cc> </nation> <nation> <name>Burundi</name> <capital>Bujumbura</capital> <population>6373002</population> <cc>bi</cc> </nation> <nation> <name>Eritrea</name> <capital>Asmara</capital> <population>4465651</population> <cc>er</cc> </nation>
</africa>
File: Transform.xslt <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" /> <xsl:template match="africa"> <xsl:text>The nations of Africa are </xsl:text> <xsl:apply-templates select="nation" /> </xsl:template> <xsl:template match="nation"> <xsl:value-of select="name" /> <xsl:if test="position() != last()">,</xsl:if> <xsl:if test="position() mod 5 = 0"> <xsl:text> </xsl:text> </xsl:if> <xsl:if test="position() = (last() - 1)">and</xsl:if> <xsl:if test="position() = last()">.</xsl:if> </xsl:template>
</xsl:stylesheet> Output: The nations of Africa are Algeria,Burundi,andEritrea.</source>
You can select the last element of given type
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<chapter>Chapter A</chapter>
<chapter>Chapter B</chapter>
<chapter>Chapter C</chapter>
<chapter>Chapter D</chapter>
</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:variable name="totalChapters">
<xsl:value-of select="//chapter[last()]"/>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="$totalChapters"/>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>Chapter D