使用XSLT根据给定的数字对XML文件进行分组



我想根据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和基于Group编号值的3。就像第二个EDICustomer应该包含3个或更少的标签。我们不知道会有多少EDICustomerLine标签和Group Number的值。还有

<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()上迭代。常量部分由第一个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>

这是所希望的。

相关内容

  • 没有找到相关文章

最新更新