输入:
<Move-Afile>
<Afile>
<HEAD010->
<Receiver>7612460142409</Receiver>
<ASN>ASN</ASN>
<Sender>8712423011212</Sender>
</HEAD010->
<HEAD020->
<DeliveryNoteNumber>6125175001</DeliveryNoteNumber>
<DeliveryNoteDate>2013-05-16</DeliveryNoteDate>
</HEAD020->
<HEAD030->
<BookingTimestamp></BookingTimestamp>
<BookingReference>F234</BookingReference>
</HEAD030->
<DELH030->
<DeliveryAddressNumber></DeliveryAddressNumber>
</DELH030->
<DELH040->
<CustomerName>Office Customer</CustomerName>
<Address1>.</Address1>
</DELH040->
<DELH050->
<Address2->Silbernstrasse 20</Address2->
<City>CH-8953 Dietikon</City>
<PostalCode>8953</PostalCode>
</DELH050->
<ITEM010-> <!--....... unbounded element.... -->
<SupplierProductCode>4401896</SupplierProductCode>
<ITEM020->
<ItemDescription>FUSION LAMINATOR BUNDLE</ItemDescription>
</ITEM020->
<ITEM030->
<EANItemCode>5028252380935</EANItemCode>
</ITEM030->
<ITEM040->
<CustomerItemNumber>13627200</CustomerItemNumber>
</ITEM040->
<ITEM060->
<MovexOrderNumber>6125175</MovexOrderNumber>
<OrderDate>13/05/13</OrderDate>
</ITEM060->
<PACK010->
<PackageType>PACKAGE/CARTON</PackageType>
</PACK010->
<PACK020->
<PackageNumber>9797</PackageNumber>
</PACK020->
<PACK050->
<PackNumber>PLT9180267</PackNumber><!-- for each <PackNumber> if <PackNumber> we are performing logic -->
</PACK050->
</ITEM010->
<ITEM010-> <!--....... unbounded element.... -->
<SupplierProductCode>4401896</SupplierProductCode>
<ITEM020->
<ItemDescription>FUSION LAMINATOR BUNDLE</ItemDescription>
</ITEM020->
<ITEM030->
<EANItemCode>5028252312343</EANItemCode>
</ITEM030->
<ITEM040->
<CustomerItemNumber>13627200</CustomerItemNumber>
</ITEM040->
<ITEM060->
<MovexOrderNumber>6125175</MovexOrderNumber>
<OrderDate>13/05/13</OrderDate>
</ITEM060->
<PACK010->
<PackageType>PACKAGE/CARTON</PackageType>
</PACK010->
<PACK020->
<PackageNumber>9797</PackageNumber>
</PACK020->
<PACK050->
<PackNumber>PLT9763411</PackNumber><!-- for each <PackNumber> if <PackNumber> we are performing logic -->
</PACK050->
</ITEM010->
<ITEM010-> <!--....... unbounded element.... -->
<SupplierProductCode>4401896</SupplierProductCode>
<ITEM020->
<ItemDescription>FUSION LAMINATOR BUNDLE</ItemDescription>
</ITEM020->
<ITEM030->
<EANItemCode>5028252888777</EANItemCode>
</ITEM030->
<ITEM040->
<CustomerItemNumber>13627200</CustomerItemNumber>
</ITEM040->
<ITEM060->
<MovexOrderNumber>6125175</MovexOrderNumber>
<OrderDate>13/05/13</OrderDate>
</ITEM060->
<PACK010->
<PackageType>PACKAGE/CARTON</PackageType>
</PACK010->
<PACK020->
<PackageNumber>9797</PackageNumber>
</PACK020->
<PACK050->
<PackNumber>PLT9180267</PackNumber><!-- for each <PackNumber> if <PackNumber> we are performing logic -->
</PACK050->
</ITEM010->
<TAIL010->
<NumberOfLines>4</NumberOfLines>
</TAIL010->
</Afile>
</Move-Afile>
每个<PackNumber>
的我们正在执行逻辑。如果<PackNumber>
不同,我们必须递增计数器,并且该节点属于同一个<PackNumber>
的节点。
output:
<A>
<target>
<d1>7612460142409</d1><!-- value of <Receiver> element -->
<d2>81</d2><!-- Hard code value -->
<d3>6125175001</d3> <!-- <DeliveryNoteNumber> value -->
<d4>F234</d4><!-- <BookingReference> value -->
<d5>Office Customer</d5><!-- <CustomerName> value -->
<d6>102</d6><!--hard code value -->
<!-- for-each <PackNumber> the following will come -->
<CPS>
<element1>2</element1><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count(starting from 2)-->
<element2>1</element2><!-- hard code value-->
</CPS>
<PAC>
<element4>111</element4><!-- hard code value-->
<element5>012</element5><!-- hard code value-->
</PAC>
<LIN>
<element6>1</element6><!-- sequence number start feom 1-->
<element7>SV</element7><!-- hard code value-->
<element8>5028252380935</element8><!-- <EANItemCode> -->
<PackNumber>PLT9180267</PackNumber>
</LIN>
<!-- same <PackNumber> is present in two nodes so another LIN element also comes under same CPS elment -->
<LIN>
<element6>2</element6><!-- sequence number incremented-->
<element7>SV</element7><!-- hard code value-->
<element8>5028252888777</element8><!-- <EANItemCode> -->
<PackNumber>PLT9180267</PackNumber>
</LIN>
<CPS>
<element1>3</element1><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count-->
<element2>1</element2><!-- hard code value-->
</CPS>
<PAC>
<element4>111</element4><!-- hard code value-->
<element5>012</element5><!-- hard code value-->
</PAC>
<LIN>
<element6>3</element6><!-- sequence number incremented-->
<element7>SV</element7><!-- hard code value-->
<element8>5028252312343</element8><!-- <EANItemCode> -->
<PackNumber>PLT9763411</PackNumber>
</LIN>
</target>
</A>
更新输入:
<Move-Afile>
<Afile>
<HEAD010->
<Receiver>7612460142409</Receiver>
<ASN>ASN</ASN>
<Sender>8712423011212</Sender>
</HEAD010->
<HEAD020->
<DeliveryNoteNumber>6125175001</DeliveryNoteNumber>
<DeliveryNoteDate>2013-05-16</DeliveryNoteDate>
</HEAD020->
<HEAD030->
<BookingTimestamp></BookingTimestamp>
<BookingReference>F234</BookingReference>
</HEAD030->
<DELH030->
<DeliveryAddressNumber></DeliveryAddressNumber>
</DELH030->
<DELH040->
<CustomerName>Office Customer</CustomerName>
<Address1>.</Address1>
</DELH040->
<DELH050->
<Address2->Silbernstrasse 20</Address2->
<City>CH-8953 Dietikon</City>
<PostalCode>8953</PostalCode>
</DELH050->
<ITEM010-> <!--....... unbounded element.... -->
<SupplierProductCode>4401896</SupplierProductCode>
<ITEM020->
<ItemDescription>FUSION LAMINATOR BUNDLE</ItemDescription>
</ITEM020->
<ITEM030->
<EANItemCode>5028252380935</EANItemCode>
</ITEM030->
<ITEM040->
<CustomerItemNumber>13627200</CustomerItemNumber>
</ITEM040->
<ITEM060->
<MovexOrderNumber>6125175</MovexOrderNumber>
<OrderDate>13/05/13</OrderDate>
</ITEM060->
<PACK010->
<PackageType>PACKAGE/CARTON</PackageType>
</PACK010->
<PACK020->
<PackageNumber>9797</PackageNumber>
</PACK020->
<PACK050->
<PackNumber>PLT9180267</PackNumber><!-- for each <PackNumber> if <PackNumber> we are performing logic -->
</PACK050->
</ITEM010->
<ITEM010-> <!--....... unbounded element.... -->
<SupplierProductCode>4401896</SupplierProductCode>
<ITEM020->
<ItemDescription>FUSION LAMINATOR BUNDLE</ItemDescription>
</ITEM020->
<ITEM030->
<EANItemCode>5028252312343</EANItemCode>
</ITEM030->
<ITEM040->
<CustomerItemNumber>13627200</CustomerItemNumber>
</ITEM040->
<ITEM060->
<MovexOrderNumber>6125175</MovexOrderNumber>
<OrderDate>13/05/13</OrderDate>
</ITEM060->
<PACK010->
<PackageType>PACKAGE/CARTON</PackageType>
</PACK010->
<PACK020->
<PackageNumber>9797</PackageNumber>
</PACK020->
<PACK050->
<PackNumber/><!-- for each <PackNumber> if <PackNumber> we are performing logic -->
</PACK050->
</ITEM010->
<ITEM010-> <!--....... unbounded element.... -->
<SupplierProductCode>4401896</SupplierProductCode>
<ITEM020->
<ItemDescription>FUSION LAMINATOR BUNDLE</ItemDescription>
</ITEM020->
<ITEM030->
<EANItemCode>5028252888777</EANItemCode>
</ITEM030->
<ITEM040->
<CustomerItemNumber>13627200</CustomerItemNumber>
</ITEM040->
<ITEM060->
<MovexOrderNumber>6125175</MovexOrderNumber>
<OrderDate>13/05/13</OrderDate>
</ITEM060->
<PACK010->
<PackageType>PACKAGE/CARTON</PackageType>
</PACK010->
<PACK020->
<PackageNumber>9797</PackageNumber>
</PACK020->
<PACK050->
<PackNumber>PLT9180267</PackNumber><!-- for each <PackNumber> if <PackNumber> we are performing logic -->
</PACK050->
</ITEM010->
<TAIL010->
<NumberOfLines>4</NumberOfLines>
</TAIL010->
</Afile>
</Move-Afile>
更新输出:
<A>
<target>
<d1>7612460142409</d1><!-- value of <Receiver> element -->
<d2>81</d2><!-- Hard code value -->
<d3>6125175001</d3> <!-- <DeliveryNoteNumber> value -->
<d4>F234</d4><!-- <BookingReference> value -->
<d5>Office Customer</d5><!-- <CustomerName> value -->
<d6>102</d6><!--hard code value -->
<!-- for-each <PackNumber> the following will come -->
<CPS>
<element1>2</element1><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count(starting from 2)-->
<element2>1</element2><!-- hard code value-->
</CPS>
<PAC>
<element4>111</element4><!-- hard code value-->
<element5>012</element5><!-- hard code value-->
</PAC>
<LIN>
<element6>1</element6><!-- sequence number start feom 1-->
<element7>SV</element7><!-- hard code value-->
<element8>5028252380935</element8><!-- <EANItemCode> -->
<PackNumber>PLT9180267</PackNumber>
</LIN>
<!-- same <PackNumber> is present in two nodes so another LIN element also comes under same CPS elment -->
<LIN>
<element6>2</element6><!-- sequence number incremented-->
<element7>SV</element7><!-- hard code value-->
<element8>5028252888777</element8><!-- <EANItemCode> -->
<PackNumber>PLT9180267</PackNumber>
</LIN>
<CPS>
<element1>3</element1><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count-->
<element2>1</element2><!-- hard code value-->
</CPS>
<PAC>
<element4>111</element4><!-- hard code value-->
<element5>012</element5><!-- hard code value-->
</PAC>
<LIN>
<element6>3</element6><!-- sequence number incremented-->
<element7>SV</element7><!-- hard code value-->
<element8>5028252312343</element8><!-- <EANItemCode> -->
<PackNumber/>
</LIN>
</target>
</A>
答案 0 :(得分:1)
此样式表将按您的要求执行。它使用key
和generate-id
一起使用每个ITEM010-
的第一个PackNumber
元素。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:key name="item-by-packnumber" match="ITEM010-" use="PACK050-/PackNumber"/>
<xsl:template match="/">
<xsl:apply-templates select="Move-Afile/Afile"/>
</xsl:template>
<xsl:template match="Afile">
<A>
<target>
<d1><xsl:value-of select="HEAD010-/Receiver"/></d1>
<d2>81</d2>
<d3><xsl:value-of select="HEAD020-/DeliveryNoteNumber"/></d3>
<d4><xsl:value-of select="HEAD030-/BookingReference"/></d4>
<d5><xsl:value-of select="DELH040-/CustomerName"/></d5>
<d6>102</d6>
<xsl:apply-templates select="ITEM010-[generate-id() = generate-id(key('item-by-packnumber', PACK050-/PackNumber)[1])]"/>
</target>
</A>
</xsl:template>
<xsl:template match="ITEM010-">
<xsl:apply-templates mode="group" select="key('item-by-packnumber', PACK050-/PackNumber)">
<xsl:with-param name="sequence" select="2+count(preceding-sibling::ITEM010-[generate-id() = generate-id(key('item-by-packnumber', PACK050-/PackNumber)[1])])"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="ITEM010-" mode="group">
<xsl:param name="sequence"/>
<CPS>
<element1><xsl:value-of select="$sequence"/></element1>
<element2>1</element2>
</CPS>
<PAC>
<element4>111</element4>
<element5>012</element5>
</PAC>
<LIN>
<element6><xsl:value-of select="position()"/></element6>
<element7>SV</element7>
<element8><xsl:value-of select="ITEM030-/EANItemCode"/></element8>
<xsl:copy-of select="PACK050-/PackNumber"/>
</LIN>
</xsl:template>
</xsl:stylesheet>
<强>输出强>
<A>
<target>
<d1>7612460142409</d1>
<d2>81</d2>
<d3>6125175001</d3>
<d4>F234</d4>
<d5>Office Customer</d5>
<d6>102</d6>
<CPS>
<element1>2</element1>
<element2>1</element2>
</CPS>
<PAC>
<element4>111</element4>
<element5>012</element5>
</PAC>
<LIN>
<element6>1</element6>
<element7>SV</element7>
<element8>5028252380935</element8>
<PackNumber>PLT9180267</PackNumber>
</LIN>
<CPS>
<element1>2</element1>
<element2>1</element2>
</CPS>
<PAC>
<element4>111</element4>
<element5>012</element5>
</PAC>
<LIN>
<element6>2</element6>
<element7>SV</element7>
<element8>5028252888777</element8>
<PackNumber>PLT9180267</PackNumber>
</LIN>
<CPS>
<element1>3</element1>
<element2>1</element2>
</CPS>
<PAC>
<element4>111</element4>
<element5>012</element5>
</PAC>
<LIN>
<element6>1</element6>
<element7>SV</element7>
<element8>5028252312343</element8>
<PackNumber>PLT9763411</PackNumber>
</LIN>
</target>
</A>
<强>更新强>
此替代方案效果更好,并生成您需要的确切XML输出。但是它有点笨拙并且使用您可能无法访问的EXSLT node-set
函数。
它的工作原理是将ITEM010-
元素排序为变量$items
,然后使用exslt:node-set($items)
从数据中进行处理,而不是直接从源中进行处理。
其他人可能会提出更好的选择。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="exsl">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:key name="item-by-packnumber" match="ITEM010-" use="PACK050-/PackNumber"/>
<xsl:template match="/">
<xsl:apply-templates select="Move-Afile/Afile"/>
</xsl:template>
<xsl:template match="*" mode="copy">
<xsl:copy>
<xsl:apply-templates mode="copy"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Afile">
<xsl:variable name="items">
<xsl:apply-templates select="ITEM010-" mode="copy">
<xsl:sort select="PACK050-/PackNumber"/>
</xsl:apply-templates>
</xsl:variable>
<A>
<target>
<d1><xsl:value-of select="HEAD010-/Receiver"/></d1>
<d2>81</d2>
<d3><xsl:value-of select="HEAD020-/DeliveryNoteNumber"/></d3>
<d4><xsl:value-of select="HEAD030-/BookingReference"/></d4>
<d5><xsl:value-of select="DELH040-/CustomerName"/></d5>
<d6>102</d6>
<xsl:apply-templates select="exsl:node-set($items)/ITEM010-"/>
</target>
</A>
</xsl:template>
<xsl:template match="ITEM010-">
<xsl:variable name="first-in-sequence" select="generate-id() = generate-id(key('item-by-packnumber', PACK050-/PackNumber)[1])"/>
<xsl:variable name="group" select="2+count(preceding-sibling::ITEM010-[generate-id() = generate-id(key('item-by-packnumber', PACK050-/PackNumber)[1])])"/>
<xsl:if test="$first-in-sequence">
<CPS>
<element1><xsl:value-of select="$group"/></element1>
<element2>1</element2>
</CPS>
<PAC>
<element4>111</element4>
<element5>012</element5>
</PAC>
</xsl:if>
<LIN>
<element6><xsl:value-of select="position()"/></element6>
<element7>SV</element7>
<element8><xsl:value-of select="ITEM030-/EANItemCode"/></element8>
<xsl:copy-of select="PACK050-/PackNumber"/>
</LIN>
</xsl:template>
</xsl:stylesheet>
<强>输出强>
<A>
<target>
<d1>7612460142409</d1>
<d2>81</d2>
<d3>6125175001</d3>
<d4>F234</d4>
<d5>Office Customer</d5>
<d6>102</d6>
<CPS>
<element1>2</element1>
<element2>1</element2>
</CPS>
<PAC>
<element4>111</element4>
<element5>012</element5>
</PAC>
<LIN>
<element6>1</element6>
<element7>SV</element7>
<element8>5028252380935</element8>
<PackNumber>PLT9180267</PackNumber>
</LIN>
<LIN>
<element6>2</element6>
<element7>SV</element7>
<element8>5028252888777</element8>
<PackNumber>PLT9180267</PackNumber>
</LIN>
<CPS>
<element1>3</element1>
<element2>1</element2>
</CPS>
<PAC>
<element4>111</element4>
<element5>012</element5>
</PAC>
<LIN>
<element6>3</element6>
<element7>SV</element7>
<element8>5028252312343</element8>
<PackNumber>PLT9763411</PackNumber>
</LIN>
</target>
</A>
答案 1 :(得分:0)
以下说明如何完成此任务:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns=""
>
<!-- Define a key so that we can fetch all packnumber nodes given a packnumber -->
<xsl:key name="packnrs" match="//PackNumber" use="text()"/>
<xsl:template match="Move-Afile">
<xsl:apply-templates select="Afile" />
</xsl:template>
<xsl:template match="Afile">
<xsl:copy>
<!-- Call process-packnrs with a set of all unique packnumbers -->
<xsl:call-template name="process-packnrs">
<xsl:with-param name="packnrs" select="//PackNumber[generate-id() = generate-id(key('packnrs', .)[1])]" />
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="process-packnrs">
<xsl:param name="count" select="2" />
<xsl:param name="packnrs" />
<xsl:param name="pos" select="1" />
<xsl:param name="seq" select="0" />
<xsl:variable name="packnr" select="$packnrs[$pos]" />
<CPS>
<element1><xsl:value-of select="$count"/></element1>
<element2>1</element2>
</CPS>
<!-- Do work for this pack number -->
<xsl:for-each select="key('packnrs', $packnr)">
<LIN>
<element6><xsl:value-of select="$seq + position()" /></element6>
<PackNumber><xsl:value-of select="." /></PackNumber>
</LIN>
</xsl:for-each>
<!-- Call for the next pack number -->
<xsl:if test="$pos < count($packnrs)">
<xsl:call-template name="process-packnrs">
<xsl:with-param name="count" select="$count + 1" />
<xsl:with-param name="packnrs" select="$packnrs" />
<xsl:with-param name="pos" select="$pos + 1" />
<xsl:with-param name="seq" select="$seq + count(key('packnrs', $packnr))" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
在xsltcake here上播放。
答案 2 :(得分:0)
经过一番思考后我建议使用这个样式表,它适用于XSLT 1.0,不使用EXSLT node-set
函数。这有点尴尬,但可能是你会得到的最好的。它的工作原理是让ITEM010-
个节点的模板以递归方式自行计算,并根据每个组的大小计算序列和组号。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:key name="item-by-packnumber" match="ITEM010-" use="PACK050-/PackNumber"/>
<xsl:template match="/">
<xsl:apply-templates select="Move-Afile/Afile"/>
</xsl:template>
<xsl:template match="Afile">
<A>
<target>
<d1><xsl:value-of select="HEAD010-/Receiver"/></d1>
<d2>81</d2>
<d3><xsl:value-of select="HEAD020-/DeliveryNoteNumber"/></d3>
<d4><xsl:value-of select="HEAD030-/BookingReference"/></d4>
<d5><xsl:value-of select="DELH040-/CustomerName"/></d5>
<d6>102</d6>
<xsl:apply-templates select="ITEM010-[1]"/>
</target>
</A>
</xsl:template>
<xsl:template match="ITEM010-">
<xsl:param name="offset" select="0"/>
<xsl:param name="group" select="2"/>
<CPS>
<element1><xsl:value-of select="$group"/></element1>
<element2>1</element2>
</CPS>
<PAC>
<element4>111</element4>
<element5>012</element5>
</PAC>
<xsl:apply-templates select="key('item-by-packnumber', PACK050-/PackNumber)" mode="group">
<xsl:with-param name="offset" select="$offset"/>
</xsl:apply-templates>
<xsl:apply-templates select="following-sibling::ITEM010-[generate-id() = generate-id(key('item-by-packnumber', PACK050-/PackNumber)[1])]">
<xsl:with-param name="offset" select="$offset + count(key('item-by-packnumber', PACK050-/PackNumber))"/>
<xsl:with-param name="group" select="$group + 1"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="ITEM010-" mode="group">
<xsl:param name="offset"/>
<LIN>
<element6><xsl:value-of select="position()+$offset"/></element6>
<element7>SV</element7>
<element8><xsl:value-of select="ITEM030-/EANItemCode"/></element8>
<PackNumber><xsl:value-of select="PACK050-/PackNumber"/></PackNumber>
</LIN>
</xsl:template>
</xsl:stylesheet>
<强>输出强>
<A>
<target>
<d1>7612460142409</d1>
<d2>81</d2>
<d3>6125175001</d3>
<d4>F234</d4>
<d5>Office Customer</d5>
<d6>102</d6>
<CPS>
<element1>2</element1>
<element2>1</element2>
</CPS>
<PAC>
<element4>111</element4>
<element5>012</element5>
</PAC>
<LIN>
<element6>1</element6>
<element7>SV</element7>
<element8>5028252380935</element8>
<PackNumber>PLT9180267</PackNumber>
</LIN>
<LIN>
<element6>2</element6>
<element7>SV</element7>
<element8>5028252888777</element8>
<PackNumber>PLT9180267</PackNumber>
</LIN>
<CPS>
<element1>3</element1>
<element2>1</element2>
</CPS>
<PAC>
<element4>111</element4>
<element5>012</element5>
</PAC>
<LIN>
<element6>3</element6>
<element7>SV</element7>
<element8>5028252312343</element8>
<PackNumber>PLT9763411</PackNumber>
</LIN>
</target>
</A>