tags:

views:

185

answers:

2

I have xml like this

<categories>
  <category>
    <Loc>India</Loc>
    <Loc>US</Loc>
    <Loc>Spain</Loc>
    <type>A</type>
    <type>B</type>
    <Cat>unknown</Cat>
    <SubCat>True</SubCat>
  </category>
</categories>

In my xsl when i m doing

<xsl:for-each select="categories/category">
All locations:<xsl:value-of select="Loc"/>
All type: <xsl:value-of select="type"/> 
</xsl:for-each>

and the result I m getting is All locations: India All type: A I want it to get all values of Loc and type All Locations: India,US,Spain All type: A,B

Can you tell me where I m getting wrong?

Thanks,

+3  A: 

Try this:

<xsl:for-each select="categories/category">
    All locations:
    <xsl:for-each select="Loc">
        <xsl:value-of select="."/>
        <xsl:if test="position() != last()">, </xsl:if>
    </xsl:for-each>
    <br />
    All type:
    <xsl:for-each select="type">
        <xsl:value-of select="."/>
        <xsl:if test="position() != last()">, </xsl:if>
    </xsl:for-each>
    <br />
</xsl:for-each>

EDIT: Note your XML sample isn't wellformed, as you got an unmatched </loc> tag.

Rubens Farias
Thanks Rubens its working absolutely fine.....
TSSS22
+1  A: 

One solution would be to incorporate templates:

<xsl:template match="categories/category">
  <xsl:text>All locations: </xsl:text>
  <xsl:apply-templates select="Loc" mode="list" />
  <xsl:text>All type: </xsl:text>
  <xsl:apply-templates select="type" mode="list" />
</xsl:template>

<xsl:template match="*" mode="list">
  <xsl:value-of select="." />
  <xsl:if test="position() != last()">, </xsl:if>
  <xsl:if test="position() = last()">&#10;</xsl:if><!-- line feed -->
</xsl:template>
Tomalak