XSLT 1.0分组基于XML中每行的开头

时间:2017-08-16 20:51:34

标签: xml xslt substring xslt-1.0 xslt-grouping

我想这个问题对于XSLT-gurus来说相当简单。在这里。

我有以下输入XML:

<?xml version="1.0" encoding="utf-8"?>
<rows xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <row>72906505066990543065    ABCDEF</row>
    <row>72916505066990608306    ABCDEF</row>
    <row>72916505066990665660    EFGHIJ</row>
    <row>72916505066990665660    EFGHIJ</row>
    <row>72916505066990684364    KLMNOP</row>
    <row>72916505066990684364    KLMNOP</row>
    <row>72916505066990684364    KLMNOP</row>
    <row>72916505066990722859    QRSTUV</row>
    <row>72916505066990722859    QRSTUV</row>
</rows>

我希望能够对行元素进行分组,以便我可以使用for-each循环对它们进行迭代。分组应该基于每行的前20个数字。

有人可以帮我解决这个问题吗?

输出XML应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<RowsFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<RowList>
  <Row>
     <LineID>
        <Identity>72906505066990543065</Identity>
     </LineID>
     <RowData>
           <Data>ABCDEF<Data/>
     </RowData>
  </Row>
  <Row>
     <LineID>
        <Identity>72916505066990608306</Identity>
     </LineID>
     <RowData>
           <Data>ABCDEF<Data/>
     </RowData>
  </Row>
  <Row>
     <LineID>
        <Identity>72916505066990665660</Identity>
     </LineID>
     <RowData>
           <Data>EFGHIJ<Data/>
     </RowData>
     <RowData>
           <Data>EFGHIJ<Data/>
     </RowData>
  </Row>
</RowList>
</RowsFile> And so on....

1 个答案:

答案 0 :(得分:2)

非常感谢迈克尔! 你指出了我正确的方向。

最后我解决了我的问题:

输入XML:

<?xml version="1.0" encoding="utf-8"?>
<rows xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>72906505066990543065    ABCDEF</row>
<row>72916505066990608306    ABCDEF</row>
<row>72916505066990665660    EFGHIJ</row>
<row>72916505066990665660    EFGHIJ</row>
<row>72916505066990684364    KLMNOP</row>
<row>72916505066990684364    KLMNOP</row>
<row>72916505066990684364    KLMNOP</row>
<row>72916505066990722859    QRSTUV</row>
<row>72916505066990722859    QRSTUV</row>
</rows>

XSLT:

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:transform version = "1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="Collection" match="row" use="substring(., 1, 20)" />                 

    <xsl:template match="rows">
        <RowsFile>
            <RowList>
            <!-- Loop all unique groups -->
            <xsl:for-each select="row[count(. | key('Collection', substring(., 1, 20))[1]) = 1]">
                <Grouped>
                    <Data>
                            <xsl:value-of select="normalize-space(substring(.,1,20))"/>
                    </Data>

                <!-- Loop all values in group-->
                <xsl:for-each select="key('Collection', substring(., 1, 20))">
                    <Value>
                        <Data><xsl:value-of select="normalize-space(substring(.,25,6))"/></Data>
                    </Value>
                </xsl:for-each>

                </Grouped>  

            </xsl:for-each>
            </RowList>
        </RowsFile>
    </xsl:template>

</xsl:transform>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<RowsFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <RowList>
    <Grouped>
      <Data>72906505066990543065</Data>
      <Value>
        <Data>ABCDEF</Data>
      </Value>
    </Grouped>
    <Grouped>
      <Data>72916505066990608306</Data>
      <Value>
        <Data>ABCDEF</Data>
      </Value>
    </Grouped>
    <Grouped>
      <Data>72916505066990665660</Data>
      <Value>
        <Data>EFGHIJ</Data>
      </Value>
      <Value>
        <Data>EFGHIJ</Data>
      </Value>
    </Grouped>
    <Grouped>
      <Data>72916505066990684364</Data>
      <Value>
        <Data>KLMNOP</Data>
      </Value>
      <Value>
        <Data>KLMNOP</Data>
      </Value>
      <Value>
        <Data>KLMNOP</Data>
      </Value>
    </Grouped>
    <Grouped>
      <Data>72916505066990722859</Data>
      <Value>
        <Data>QRSTUV</Data>
      </Value>
      <Value>
        <Data>QRSTUV</Data>
      </Value>
    </Grouped>
  </RowList>
</RowsFile>