与组进行xslt转换

时间:2010-08-31 09:55:53

标签: xml xslt

我有一个像这样的xml:

<Ownership_Shareholders_S>
        <Ownership_Shareholders>
            <CUSTOMER_31>A</CUSTOMER_31>
            <PERCENT_NAME_OF_OWNERS_32>40%</PERCENT_NAME_OF_OWNERS_32>
        </Ownership_Shareholders>
        <Ownership_Shareholders COLL_ID="1">
            <CUSTOMER_31>A</CUSTOMER_31>
            <PERCENT_NAME_OF_OWNERS_32>30%</PERCENT_NAME_OF_OWNERS_32>
        </Ownership_Shareholders>
        <Ownership_Shareholders COLL_ID="2">
            <CUSTOMER_31>B</CUSTOMER_31>
            <PERCENT_NAME_OF_OWNERS_32>20%</PERCENT_NAME_OF_OWNERS_32>
        </Ownership_Shareholders>
        <Ownership_Shareholders COLL_ID="3">
            <CUSTOMER_31>B</CUSTOMER_31>
            <PERCENT_NAME_OF_OWNERS_32>29%</PERCENT_NAME_OF_OWNERS_32>
        </Ownership_Shareholders>
    </Ownership_Shareholders_S>

我需要帮助来编写一个xslt来转换以下

中的这些数据
A 40%
  30%
B 20%
  29%

因此,我需要按客户字段对数据进行分组。怎么办呢?

2 个答案:

答案 0 :(得分:3)

以下XSLT 2.0转换将按您的要求执行:

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

  <xsl:output method="text"/>

  <xsl:template match="@*|node()">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:template>

  <xsl:template match="Ownership_Shareholders_S">
    <xsl:for-each-group select="Ownership_Shareholders" group-by="CUSTOMER_31">
      <xsl:value-of select="current-grouping-key()"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="current-group()/PERCENT_NAME_OF_OWNERS_32"
                    separator="&#x0a;  "/>
      <xsl:text>&#x0a;</xsl:text>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>

<强>输出:

A 40%
  30%
B 20%
  29%

如果您只能使用XSLT 1.0,请告诉我们!


这是一个XSLT 1.0转换。通常,在XSLT 2.0中使用for-each-group时,您通常可以在XSLT 1.0中使用Muenchian grouping method执行相同的操作:

<强> XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text"/>
  <xsl:key name="customer" match="Ownership_Shareholders" use="CUSTOMER_31"/>

  <xsl:template match="Ownership_Shareholders_S">
    <xsl:for-each select="Ownership_Shareholders[
        generate-id() = generate-id(key('customer', CUSTOMER_31)[1])
      ]">
      <xsl:value-of select="CUSTOMER_31"/>
      <xsl:text> </xsl:text>
      <xsl:for-each select="key('customer', CUSTOMER_31)">
        <xsl:value-of select="PERCENT_NAME_OF_OWNERS_32"/>
        <xsl:text>&#x0a;</xsl:text>
        <xsl:if test="position() != last()">
          <xsl:text>  </xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:2)

只是为了好玩,紧凑的XSLT 1.0:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:key name="kCustomerByValue" match="CUSTOMER_31" use="."/>
    <xsl:template match="text()"/>
    <xsl:template match="CUSTOMER_31[count(.|key('kCustomerByValue',.)[1])=1]">
        <xsl:for-each select="key('kCustomerByValue',.)">
            <xsl:value-of select="concat(substring(concat(.,' '),
                                                   1+(position()!=1),
                                                   1),
                                         ' ',
                                         ../PERCENT_NAME_OF_OWNERS_32,
                                         '&#xA;')"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

一行XPath 2.0:

string-join(for $customer
            in distinct-values(/Ownership_Shareholders_S
                                /Ownership_Shareholders
                                 /CUSTOMER_31)
            return /Ownership_Shareholders_S
                    /Ownership_Shareholders[CUSTOMER_31=$customer]
                     /PERCENT_NAME_OF_OWNERS_32
                      /concat(if (position()=1) 
                              then $customer 
                              else replace($customer,'.',' '),
                              ' ',.),
            '&#xA;')