没有使用xsl获取xml的多个值

时间:2010-01-05 15:40:04

标签: xml xslt

我有像这样的xml

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

我的xsl当我在做

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

我得到的结果是 所有地点:印度 所有类型:A 我希望它获取Loc和type的所有值 所有地点:印度,美国,西班牙 所有类型:A,B

你能告诉我哪里出错了吗?

谢谢,

2 个答案:

答案 0 :(得分:3)

试试这个:

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

编辑:注意您的XML示例格式不正确,因为您获得了无与伦比的</loc>代码。

答案 1 :(得分:2)

一种解决方案是合并模板:

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