使用元素'code'的键值进行XSLT分组

时间:2015-07-13 07:20:33

标签: xml xslt

我是XSLT的新手。我需要根据供应商代码的值对以下XML进行分组。

这里是输入XML:

<ns0:File xmlns:ns0="urn:com:testing">
  <Header>
    <recordType>1</recordType>
    <communityCode>commu</communityCode>
    <date>20090710</date>
    <time>0200</time>
    <communityName>comm</communityName>
    <fileIdentifier>xx1</fileIdentifier>
    <filler>000#</filler>
  </Header>
  <detail>
    <recordType>5</recordType>
    <supplierCode>1</supplierCode>
    <supplierName>xxx LTD</supplierName>
    <email>xxx@xx.com</email>
    <phone/>
    <mobile>123456</mobile>
    <fax/>
    <supplierGroup>123</supplierGroup>
    <flag/>
    <filler>10</filler>
  </detail>
  <Address>
      <recordType>6</recordType>
      <supplierCode>1</supplierCode>
      <street1>PO BOX</street1>
      <street2/>
      <city>city 1</city>
      <postcode>3818</postcode>
      <state>st 1</state>
      <filler>#</filler>
   </Address>
  <BankDetails>
    <recordType>7</recordType>
    <supplierCode>1</supplierCode>
    <BSB>0001</BSB>
    <AccountNumber>000001</AccountNumber>
    <flag>1</flag>
    <defaultAccount>#</defaultAccount>
    <filler/>
  </BankDetails>
  <BankDetails>
    <recordType>7</recordType>
    <supplierCode>1</supplierCode>
    <BSB>0002</BSB>
    <AccountNumber>000002</AccountNumber>
    <flag>1</flag>
    <defaultAccount>#</defaultAccount>
    <filler/>
  </BankDetails>
  <detail>
    <recordType>5</recordType>
    <supplierCode>2</supplierCode>
    <supplierName>yyy</supplierName>
    <email/>
    <phone/>
    <mobile/>
    <fax/>
    <supplierGroup>321</supplierGroup>
    <flag/>
    <filler>01Error in Sup</filler>
  </detail>
  <Address>
      <recordType>6</recordType>
      <supplierCode>2</supplierCode>
      <street1>PO BOX 111</street1>
      <street2/>
      <city>city2</city>
      <postcode>2121</postcode>
      <state>st 2</state>
      <filler>#</filler>
   </Address>
  <detail>
    <recordType>5</recordType>
    <supplierCode>3</supplierCode>
    <supplierName>PTY LTD</supplierName>
    <email>70119769185</email>
    <phone/>
    <mobile>03 33333</mobile>
    <fax/>
    <supplierGroup>0333333</supplierGroup>
    <flag/>
    <filler>00</filler>
  </detail>
  <Address>
      <recordType>6</recordType>
      <supplierCode>3</supplierCode>
      <street1>LOCKED BAG </street1>
      <street2/>
      <city>city3</city>
      <postcode>30000</postcode>
      <state>st 3</state>
      <filler>#</filler>
   </Address>
  <detail>
    <recordType>5</recordType>
    <supplierCode>4</supplierCode>
    <supplierName>RIVA</supplierName>
    <email>53004892835</email>
    <phone/>
    <mobile>4535444</mobile>
    <fax/>
    <supplierGroup>5345</supplierGroup>
    <flag/>
    <filler>00</filler>
  </detail>
  <Address>
      <recordType>6</recordType>
      <supplierCode>4</supplierCode>
      <street1>PO BOX 224</street1>
      <street2/>
      <city>444</city>
      <postcode>23433</postcode>
      <state>N3</state>
      <filler>#</filler>
   </Address>

  <BankDetails>
    <recordType>7</recordType>
    <supplierCode>4</supplierCode>
    <BSB>21</BSB>
    <AccountNumber>22</AccountNumber>
    <flag>1</flag>
    <defaultAccount>#</defaultAccount>
    <filler/>
  </BankDetails>
  <BankDetails>
    <recordType>7</recordType>
    <supplierCode>4</supplierCode>
    <BSB>31</BSB>
    <AccountNumber>33</AccountNumber>
    <flag>1</flag>
    <defaultAccount>#</defaultAccount>
    <filler/>
  </BankDetails>

  <Trailer>
    <recordType>9</recordType>
    <countDetailRecord>00000013</countDetailRecord>
    <countAddressRecord>00000013</countAddressRecord>
    <countBankRecord>00000001</countBankRecord>
    <countUserDetails>00000000</countUserDetails>
    <filler>#</filler>
  </Trailer>
</ns0:File>

我希望结果如下:

<ns0:File xmlns:ns0="urn:com:testing">
  <Header>
    <recordType>1</recordType>
    <communityCode>commu</communityCode>
    <date>20090710</date>
    <time>0200</time>
    <communityName>comm</communityName>
    <fileIdentifier>xx1</fileIdentifier>
    <filler>000#</filler>
  </Header>
  <supplier>
    <detail>
      <recordType>5</recordType>
      <supplierCode>1</supplierCode>
      <supplierName>xxx LTD</supplierName>
      <email>xxx@xx.com</email>
      <phone/>
      <mobile>123456</mobile>
      <fax/>
      <supplierGroup>123</supplierGroup>
      <flag/>
      <filler>10</filler>
    </detail>
    <Address>
      <recordType>6</recordType>
      <supplierCode>1</supplierCode>
      <street1>PO BOX</street1>
      <street2/>
      <city>city 1</city>
      <postcode>3818</postcode>
      <state>st 1</state>
      <filler>#</filler>
   </Address>
    <BankDetails>
      <recordType>7</recordType>
      <supplierCode>1</supplierCode>
      <BSB>0001</BSB>
      <AccountNumber>000001</AccountNumber>
      <flag>1</flag>
      <defaultAccount>#</defaultAccount>
      <filler/>
    </BankDetails>
    <BankDetails>
      <recordType>7</recordType>
      <supplierCode>1</supplierCode>
      <BSB>0002</BSB>
      <AccountNumber>000002</AccountNumber>
      <flag>1</flag>
      <defaultAccount>#</defaultAccount>
      <filler/>
    </BankDetails>
  </supplier>
  <supplier>
    <detail>
      <recordType>5</recordType>
      <supplierCode>2</supplierCode>
      <supplierName>yyy</supplierName>
      <email/>
      <phone/>
      <mobile/>
      <fax/>
      <supplierGroup>321</supplierGroup>
      <flag/>
      <filler>01Error in Sup</filler>
    </detail>
    <Address>
      <recordType>6</recordType>
      <supplierCode>2</supplierCode>
      <street1>PO BOX 111</street1>
      <street2/>
      <city>city2</city>
      <postcode>2121</postcode>
      <state>st 2</state>
      <filler>#</filler>
   </Address>
  </supplier>
  <supplier>
    <detail>
      <recordType>5</recordType>
      <supplierCode>3</supplierCode>
      <supplierName>PTY LTD</supplierName>
      <email>70119769185</email>
      <phone/>
      <mobile>03 33333</mobile>
      <fax/>
      <supplierGroup>0333333</supplierGroup>
      <flag/>
      <filler>00</filler>
    </detail>
    <Address>
      <recordType>6</recordType>
      <supplierCode>3</supplierCode>
      <street1>LOCKED BAG </street1>
      <street2/>
      <city>city3</city>
      <postcode>30000</postcode>
      <state>st 3</state>
      <filler>#</filler>
   </Address>
  </supplier>
  <supplier>
    <detail>
      <recordType>5</recordType>
      <supplierCode>4</supplierCode>
      <supplierName>RIVA</supplierName>
      <email>53004892835</email>
      <phone/>
      <mobile>4535444</mobile>
      <fax/>
      <supplierGroup>5345</supplierGroup>
      <flag/>
      <filler>00</filler>
    </detail>
    <Address>
      <recordType>6</recordType>
      <supplierCode>4</supplierCode>
      <street1>PO BOX 224</street1>
      <street2/>
      <city>444</city>
      <postcode>23433</postcode>
      <state>N3</state>
      <filler>#</filler>
   </Address>

    <BankDetails>
      <recordType>7</recordType>
      <supplierCode>4</supplierCode>
      <BSB>21</BSB>
      <AccountNumber>22</AccountNumber>
      <flag>1</flag>
      <defaultAccount>#</defaultAccount>
      <filler/>
    </BankDetails>
    <BankDetails>
      <recordType>7</recordType>
      <supplierCode>4</supplierCode>
      <BSB>31</BSB>
      <AccountNumber>33</AccountNumber>
      <flag>1</flag>
      <defaultAccount>#</defaultAccount>
      <filler/>
    </BankDetails>
  </supplier>
  <Trailer>
    <recordType>9</recordType>
    <countDetailRecord>0000004</countDetailRecord>
    <countAddressRecord>0000004</countAddressRecord>
    <countBankRecord>00000004</countBankRecord>
    <countUserDetails>00000000</countUserDetails>
    <filler>#</filler>
  </Trailer>
</ns0:File>

基本上我需要为每个供应商介绍供应商标签。 需要使用XSLT 1.0进行转换,但我不知道使用XSLT。

1 个答案:

答案 0 :(得分:1)

以下是使用for-each-group的XSLT 2.0样式表:

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

    <xsl:strip-space elements="*"/>
    <xsl:output  indent="yes"/>

    <xsl:template match="/*">
        <xsl:copy>
            <xsl:copy-of select="Header"/>
            <xsl:for-each-group select="* except (Header, Trailer)" group-by="supplierCode">
                <supplier>
                    <xsl:copy-of select="current-group()"/>
                </supplier>
            </xsl:for-each-group>
            <xsl:copy-of select="Trailer"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>