我必须使用以下格式的大型xml。
<First>
<Second Argument="12" Argument2="34" >
<Order InvOrg="123" Identifier="3580456" >
<SupplyDetails Type="TRACK" Quantity="7" Demand="ONHAND" />
</Order>
<Order InvOrg="Test" Identifier="3580457" >
<SupplyDetails Type="TRACK" Quantity="15" Demand="ONHAND" />
</Order>
<Order InvOrg="123" Identifier="3580458" >
<SupplyDetails Type="TRACK" Quantity="17" Demand="ONHAND" />
</Order>
<Order InvOrg="123" Identifier="3580459" >
<SupplyDetails Type="TRACK" Quantity="10" Demand="ONHAND" />
</Order>
<Order InvOrg="123" Identifier="2888144" >
<SupplyDetails Type="TRACK" Quantity="3" Demand="ONHAND" />
</Order>
<Order InvOrg="123" Identifier="2888147" >
<SupplyDetails Type="TRACK" Quantity="9" Demand="ONHAND" />
<Order>
<Order InvOrg="123" Identifier="2749622" >
<SupplyDetails Type="TRACK" Quantity="142" Demand="ONHAND" />
</Order>
</Second>
</First>
我需要将其转换为以下格式
<First>
<Second Argument="12" Argument2="34" >
<Order InvOrg="123" Identifier="3580456" >
<SupplyDetails Type="TRACK" Quantity="7" Demand="ONHAND" />
</Order>
<Order InvOrg="Test" Identifier="3580457" >
<SupplyDetails Type="TRACK" Quantity="15" Demand="ONHAND" />
</Order>
<Order InvOrg="123" Identifier="3580458" >
<SupplyDetails Type="TRACK" Quantity="17" Demand="ONHAND" />
</Order>
</Second>
</First>
<First>
<Second Argument="12" Argument2="34" >
<Order InvOrg="123" Identifier="3580459" >
<SupplyDetails Type="TRACK" Quantity="10" Demand="ONHAND" />
</Order>
<Order InvOrg="123" Identifier="2888144" >
<SupplyDetails Type="TRACK" Quantity="3" Demand="ONHAND" />
</Order>
<Order InvOrg="123" Identifier="2888147" >
<SupplyDetails Type="TRACK" Quantity="9" Demand="ONHAND" />
<Order>
<Order InvOrg="123" Identifier="2749622" >
<SupplyDetails Type="TRACK" Quantity="142" Demand="ONHAND" />
</Order>
</Second>
</First>
在xslt 2.0中,可以使用以下代码实现..
<xsl:stylesheet
version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/First">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Second">
<xsl:for-each-group select="Order" group-starting-with="Order[(position() -1)mod 3 = 0]">
<First>
<Second>
<xsl:copy-of select="current-group()"/>
</Second>
</First>
</xsl:for-each-group>
</xsl:template>
我的项目只使用xslt 1.0,所以我需要将上面的逻辑更改为1.0。我怎样才能在xslt 1.0中实现相同的逻辑
答案 0 :(得分:1)
以下XSLT使用递归来反复&#34;迭代&#34;超过配置大小的订单和拆分(例如3)。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<!-- set the elements per block -->
<xsl:param name="block_length" select="3"/>
<!-- initialize the "iteration" over the Order elements by selecting all
of them as the "child_list" -->
<xsl:template match="/First">
<root>
<xsl:call-template name="split">
<xsl:with-param name="child_list" select="Second/Order"/>
</xsl:call-template>
</root>
</xsl:template>
<!-- handle a list of orders, split off the first three and then "iterate" over
the remaining orders by recursion -->
<xsl:template name="split">
<xsl:param name="child_list"/>
<xsl:if test="count($child_list) > 0">
<!-- split off the first three -->
<First>
<Second>
<xsl:copy-of select="$child_list[position() <= $block_length]"/>
</Second>
</First>
<!-- handle the remaining entries by recursion -->
<xsl:call-template name="split">
<xsl:with-param name="child_list" select="$child_list[position() > $block_length]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
产生这个结果
<?xml version="1.0" encoding="UTF-8"?>
<root>
<First>
<Second>
<Order InvOrg="123" Identifier="3580456">
<SupplyDetails Type="TRACK" Quantity="7" Demand="ONHAND"/>
</Order>
<Order InvOrg="Test" Identifier="3580457">
<SupplyDetails Type="TRACK" Quantity="15" Demand="ONHAND"/>
</Order>
<Order InvOrg="123" Identifier="3580458">
<SupplyDetails Type="TRACK" Quantity="17" Demand="ONHAND"/>
</Order>
</Second>
</First>
<First>
<Second>
<Order InvOrg="123" Identifier="3580459">
<SupplyDetails Type="TRACK" Quantity="10" Demand="ONHAND"/>
</Order>
<Order InvOrg="123" Identifier="2888144">
<SupplyDetails Type="TRACK" Quantity="3" Demand="ONHAND"/>
</Order>
<Order InvOrg="123" Identifier="2888147">
<SupplyDetails Type="TRACK" Quantity="9" Demand="ONHAND"/>
</Order>
</Second>
</First>
<First>
<Second>
<Order InvOrg="123" Identifier="2749622">
<SupplyDetails Type="TRACK" Quantity="142" Demand="ONHAND"/>
</Order>
</Second>
</First>
</root>
我认为此解决方案的一小部分具有复杂度O(n*n)
,其中n
是订单的初始数量,因此您可能遇到包含大量订单号的性能问题。