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())"
<source lang="xml">
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</source>
last() function is in for-each element. The context is therefore all selected chapters.
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<chapter>Chapter A</chapter> <chapter>Chapter B</chapter> <chapter>Chapter C</chapter> <chapter>Chapter D</chapter>
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: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"?>Chapter A(last = 4) |
Chapter B(last = 4) |
Chapter C(last = 4) |
Chapter D(last = 4) |
last() function is in template. The context is therefore a single chapter element.
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<chapter>Chapter A</chapter> <chapter>Chapter B</chapter> <chapter>Chapter C</chapter> <chapter>Chapter D</chapter>
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:apply-templates select="."/> |
</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"?>Chapter A(last = 1) |
Chapter B(last = 1) |
Chapter C(last = 1) |
Chapter D(last = 1) |
last() returns a number equal to the context size from the expression evaluation context
<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"?>
last()- Returns a value equal to the context size
<source lang="xml">
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"/> </source> <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
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<chapter>Chapter A</chapter> <chapter>Chapter B</chapter> <chapter>Chapter C</chapter> <chapter>Chapter D</chapter>
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</source>