I have searched for an answer to this with no luck. I’m sure I have overlooked the answer somewhere. However, I am trying to print/display a subset of xml values as a comma delimited list. Here is an example of what I am trying to do;
XML Doc.
<vehicle>
<car new="y">
<yr>2012</yr>
<make>Ford</make>
<model>Mustang</model>
<color>Blue</color>
</car>
<car new="y">
<yr>2012</yr>
<make>Chevy</make>
<model>Camaro</model>
<color>Red</color>
</car>
<car new="y">
<yr>2012</yr>
<make>Subaru</make>
<model>Impreza</model>
<color>White</color>
</car>
<car new="n">
<yr>2000</yr>
<make>Toyota</make>
<model>Tacoma</model>
<color>Silver</color>
</car>
<car new="n">
<yr>1998</yr>
<make>Dodge</make>
<model>Durango</model>
<color>Green</color>
</car>
</vehicle>
XSL DOC..
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/" >
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles2.css" />
</head>
<body>
<h2> New Cars </h2>
<p>
<xsl:for-each select="vehicle/car">
<xsl:sort select="./yr" data-type="text" order="ascending" />
<xsl:if test="./@new='y'">
<xsl:value-of select="yr" />
<xsl:text> </xsl:text>
<xsl:value-of select="make" />
<xsl:text> - </xsl:text>
<xsl:value-of select="model" />
</xsl:if>
</xsl:for-each>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
So, in this example I want to select all “new” cars and place them in a comma delimited list and not have a comma after the last item in the list.
I can’t use xsl:if test="position() != last()> since the position of the “last” “new” car may not be the “last” position in the xml. I would prefer this to be done in xml version 1.0.
Any Suggestions or Ideas? Thanks in advance!
Example output:
2012 Ford Mustang, 2012 Chevy Camaro, 2012 Subaru Impreza
Use
instead of getting them all and using an if test. Then you can use last().