开发者

not getting multiple values of xml using xsl

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>
</categori开发者_开发技巧es>

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,


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜