我想基于XML标记值对XML文件进行分组。以下是我的输入XML。
<Root>
<GroupNumber>3</GroupNumber>
<EDICustomer>
<Company>IWS</Company>
<Customer>150</Customer>
<CreationDate>2020-03-15T10:29:08.813</CreationDate>
<CreationTime>2020-03-15T10:29:08.813</CreationTime>
<EDICustomerLine>
<Barcode>447896130245</Barcode>
<ItemDescription>Medium</ItemDescription>
<Warehouse>B01</Warehouse>
<Status>12</Status>
</EDICustomerLine>
<EDICustomerLine>
<Barcode>447896130245</Barcode>
<ItemDescription>Medium</ItemDescription>
<Warehouse>B01</Warehouse>
<Status>12</Status>
</EDICustomerLine>
<EDICustomerLine>
<Barcode>447896130245</Barcode>
<ItemDescription>Medium</ItemDescription>
<Warehouse>B01</Warehouse>
<Status>12</Status>
</EDICustomerLine>
<EDICustomerLine>
<Barcode>447896130245</Barcode>
<ItemDescription>Medium</ItemDescription>
<Warehouse>B01</Warehouse>
<Status>12</Status>
</EDICustomerLine>
</EDICustomer>
</Root>
我不知道会有多少EDICustomerLine标签。如果GroupNumber值大于EDICustomerLine标签的数量,则保留EDICustomer子标签不变,但如果GroupNumber小于EDICustomerLine标签,我们必须根据GroupNumber标签的值拆分EDICustomerLine标签。因此,在这种情况下,预期的XML将如下所示。
<Root>
<EDICustomer>
<Company>IWS</Company>
<Customer>150</Customer>
<CreationDate>2020-03-15T10:29:08.813</CreationDate>
<CreationTime>2020-03-15T10:29:08.813</CreationTime>
<EDICustomerLine>
<Barcode>447896130245</Barcode>
<ItemDescription>Medium</ItemDescription>
<Warehouse>B01</Warehouse>
<Status>12</Status>
</EDICustomerLine>
<EDICustomerLine>
<Barcode>447896130245</Barcode>
<ItemDescription>Medium</ItemDescription>
<Warehouse>B01</Warehouse>
<Status>12</Status>
</EDICustomerLine>
<EDICustomerLine>
<Barcode>447896130245</Barcode>
<ItemDescription>Medium</ItemDescription>
<Warehouse>B01</Warehouse>
<Status>12</Status>
</EDICustomerLine>
</EDICustomer>
<EDICustomer>
<Company>IWS</Company>
<Customer>150</Customer>
<CreationDate>2020-03-15T10:29:08.813</CreationDate>
<CreationTime>2020-03-15T10:29:08.813</CreationTime>
<EDICustomerLine>
<Barcode>447896130245</Barcode>
<ItemDescription>Medium</ItemDescription>
<Warehouse>B01</Warehouse>
<Status>12</Status>
</EDICustomerLine>
</EDICustomer>
</Root>
第一个EDICustomer父标记基于组号值包含Company,Customer,CreationDate,CreationTime和3。这样,第二个EDICustomer应该包含3个或更少的标签。我们不知道将有多少EDICustomerLine标签和组号的值。也
<Company>IWS</Company>
<Customer>150</Customer>
<CreationDate>2020-03-15T10:29:08.813</CreationDate>
<CreationTime>2020-03-15T10:29:08.813</CreationTime>
上面的这一部分应该在所有EDICustomer组中重复(如果我们找到一种基于组号循环EDICustomerLine的方法,则可以进行此部分)。
首先,我想知道在XSLT中是否可能出现这种情况。
如果可能的话,你们可以帮我吗?
您可以使用以下XSLT-2.0样式表获得所需的结果。它将<EDICustomerLine>
元素分组为GroupNumber
,然后在上迭代current-group()
。常量部分由first创建xsl:copy-of
。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="text()" />
<!-- identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="Root">
<xsl:variable name="max" select="GroupNumber" />
<xsl:copy>
<!-- Handle the "zero-items" situation" -->
<xsl:if test="count(EDICustomer/EDICustomerLine) = 0">
<EDICustomer>
<xsl:copy-of select="EDICustomer[1]/Company | EDICustomer[1]/Customer | EDICustomer[1]/CreationDate | EDICustomer[1]/CreationTime" />
</EDICustomer>
</xsl:if>
<xsl:for-each-group select="EDICustomer/EDICustomerLine" group-by="(position()-1) idiv $max">
<EDICustomer>
<!-- Add a <GroupNr> element with the index of the group -->
<GroupNr><xsl:value-of select="position()" /></GroupNr>
<xsl:copy-of select="../Company | ../Customer | ../CreationDate | ../CreationTime" />
<xsl:copy-of select="current-group()" />
</EDICustomer>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
其结果是:
<Root>
<EDICustomer>
<GroupNr>1</GroupNr>
<Company>IWS</Company>
<Customer>150</Customer>
<CreationDate>2020-03-15T10:29:08.813</CreationDate>
<CreationTime>2020-03-15T10:29:08.813</CreationTime>
<EDICustomerLine>
<Barcode>447896130245</Barcode>
<ItemDescription>Medium</ItemDescription>
<Warehouse>B01</Warehouse>
<Status>12</Status>
</EDICustomerLine>
<EDICustomerLine>
<Barcode>447896130245</Barcode>
<ItemDescription>Medium</ItemDescription>
<Warehouse>B01</Warehouse>
<Status>12</Status>
</EDICustomerLine>
<EDICustomerLine>
<Barcode>447896130245</Barcode>
<ItemDescription>Medium</ItemDescription>
<Warehouse>B01</Warehouse>
<Status>12</Status>
</EDICustomerLine>
</EDICustomer>
<EDICustomer>
<GroupNr>2</GroupNr>
<Company>IWS</Company>
<Customer>150</Customer>
<CreationDate>2020-03-15T10:29:08.813</CreationDate>
<CreationTime>2020-03-15T10:29:08.813</CreationTime>
<EDICustomerLine>
<Barcode>447896130245</Barcode>
<ItemDescription>Medium</ItemDescription>
<Warehouse>B01</Warehouse>
<Status>12</Status>
</EDICustomerLine>
</EDICustomer>
</Root>
这是所希望的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句