How can I make a comma-separated list of xml values? xml version 1.0

I was looking for the answer to this without any luck. I'm sure I forgot the answer somewhere. However, I am trying to print / display a subset of xml values ​​as a comma-separated list. Here is an example of what I'm 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 the β€œnew” cars and put them in a comma-separated list and not have a comma after the last item in the list.

I cannot use xsl:if test="position() != last()> , since the position of the" last "" new "car cannot be the" last "position in xml. I would prefer it to be done in xml version 1.0.

Any suggestions or ideas? Thanks in advance!

Output Example:

 2012 Ford Mustang, 2012 Chevy Camaro, 2012 Subaru Impreza 
+4
source share
1 answer

Using

 <xsl:for-each select="vehicle/car[@new='y']"> 

instead of getting them all and using the if test. Then you can use last ().

+2
source

Source: https://habr.com/ru/post/1381835/


All Articles