需要帮助...在XSLT中分组

时间:2014-04-20 12:51:55

标签: xslt

请使用XSLT1.0提供Muenchian Grouping的代码....

<list numOfItems="3">
<item>
<field dataType="double" name="DOWNPAYMENT">0</field>
</item>
<item>
<field dataType="double" name="DOWNPAYMENT">1</field>
</item>
<item>
<field dataType="double" name="DOWNPAYMENT">2</field>
</item>
</list>
<list numOfItems="3">
<item>
<field dataType="double" name="BALLOONPAYMENT">0</field>
</item>
<item>
<field dataType="double" name="BALLOONPAYMENT">1</field>
</item>
<item>
<field dataType="double" name="BALLOONPAYMENT">2</field>
</item>
</list>

输出应该是......

<campaigns>
<downpayment>0</downpayment>
<Ballonpayment>0</Ballonpayment>
</campaigns>     
<campaigns>
<downpayment>1</downpayment>
<Ballonpayment>1</Ballonpayment>
</campaigns>     
<campaigns>
<downpayment>2</downpayment>
<Ballonpayment>2</Ballonpayment>
</campaigns>

1 个答案:

答案 0 :(得分:0)

定义你的密钥:

<xsl:key name="value" match="item/field" use="."/>

然后处理

<xsl:template match="/">
  <xsl:apply-templates select="//list/item/field[generate-id() = generate-id(key('value', .)[1])]" mode="group"/>
</xsl:template>

<xsl:template match="item/field" mode="group">
  <campaigns>
    <xsl:apply-templates select="key('value', .)"/>
  </campaigns>
</xsl:template>

<xsl:template match="item/field[@name= 'BALLOONPAYMENT']">
  <Ballonpayment>
    <xsl:value-of select="."/>
  </Ballonpayment>
</xsl:template>

<xsl:template match="item/field[@name= 'DOWNPAYMENT']">
  <downpayment>
    <xsl:value-of select="."/>
  </downpayment>
</xsl:template>