我想这个问题对于 SO 上的 XSLT 大师来说相当简单。
我有以下输入 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....
非常感谢迈克尔! 你为我指出了正确的方向。
最后我像这样解决了我的问题:
输入 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>