Xml 用XSLT连接多个重复元素
我有以下示例xml数据。这种情况是,当type=#时,productNo元素必须与type元素值和number元素值连接。连接的输出必须与该orderItem记录中的每个serialNumber元素连接 最后的要求是: 1.当type元素为“#”时,productNo与每个type元素和number元素的串联应与每个orderItem记录中的每个serialNumber元素串联。 2.当type元素没有“#”时,productNo应该与每个orderItem记录中的每个serialNumber元素连接起来Xml 用XSLT连接多个重复元素,xml,xslt,tibco,Xml,Xslt,Tibco,我有以下示例xml数据。这种情况是,当type=#时,productNo元素必须与type元素值和number元素值连接。连接的输出必须与该orderItem记录中的每个serialNumber元素连接 最后的要求是: 1.当type元素为“#”时,productNo与每个type元素和number元素的串联应与每个orderItem记录中的每个serialNumber元素串联。 2.当type元素没有“#”时,productNo应该与每个orderItem记录中的每个serialNumber元
<orderItems>
<orderItem itemNo="0100" sapItemNo="10">
<productNo>WK302EA</productNo>
<itemShipDetails>
<itemShipDetail>
<serialNumber>CZC132BM61</serialNumber>
</itemShipDetail>
<itemShipDetail>
<serialNumber>CZC1331JR2</serialNumber>
</itemShipDetail>
<itemShipDetail>
<serialNumber>CZC1331JR3</serialNumber>
</itemShipDetail>
</itemShipDetails>
<options>
<option ln="01" type="" sapItemNo="10">
<number>WK302EA</number>
</option>
<option ln="02" type="#" sapItemNo="10">
<number>ABN</number>
</option>
<option ln="03" type="#" sapItemNo="10">
<number>ASZ</number>
</option>
</options>
</orderItem>
<orderItem itemNo="0200" sapItemNo="20">
<productNo>VY623AA</productNo>
<itemShipDetails>
<itemShipDetail>
<serialNumber>CN3129300D</serialNumber>
</itemShipDetail>
<itemShipDetail>
<serialNumber>CN3129300Z</serialNumber>
</itemShipDetail>
<itemShipDetail>
<serialNumber>CN3129306S</serialNumber>
</itemShipDetail>
<itemShipDetail>
<serialNumber>CN312930LM</serialNumber>
</itemShipDetail>
</itemShipDetails>
<options>
<option ln="04" type="" sapItemNo="20">
<number>VY623AA</number>
</option>
<option ln="05" type="#" sapItemNo="20">
<number>ABN</number>
</option>
</options>
</orderItem>
<orderItem itemNo="0300" sapItemNo="30">
<productNo>VY623AS</productNo>
<itemShipDetails>
<itemShipDetail>
<serialNumber>CN3129300X</serialNumber>
</itemShipDetail>
<itemShipDetail>
<serialNumber>CN3129300P</serialNumber>
</itemShipDetail>
</itemShipDetails>
<options>
<option ln="06" type="" sapItemNo="30">
<number>VY623AS</number>
</option>
<option ln="07" type="M" sapItemNo="30">
<number>ABC</number>
</option>
</options>
</orderItem>
</orderItems>
WK302EA
CZC132BM61
CZC1331JR2
CZC1331JR3
WK302EA
荷兰银行
ASZ
VY623AA
3129300D元
CN3129300Z
CN3129306S
CN312930LM
VY623AA
荷兰银行
VY623AS
3129300X元
3129300P元
VY623AS
基础知识
预期产出为:
<orders>
<serialNO>WK302EA#ABN|CZC132BM61</serialNO>
<serialNO>WK302EA#ABN|CZC1331JR2</serialNO>
<serialNO>WK302EA#ABN|CZC1331JR3</serialNO>
<serialNO>WK302EA#ASZ|CZC132BM61</serialNO>
<serialNO>WK302EA#ASZ|CZC1331JR2</serialNO>
<serialNO>WK302EA#ASZ|CZC1331JR3</serialNO>
<serialNO>VY623AA#ABN|CN3129300D</serialNO>
<serialNO>VY623AA#ABN|CN3129300Z</serialNO>
<serialNO>VY623AA#ABN|CN3129306S</serialNO>
<serialNO>VY623AA#ABN|CN312930LM</serialNO>
<serialNO>VY623AS|CN3129300X</serialNO>
<serialNO>VY623AS|CN3129300P</serialNO>
</orders>
WK302EA#ABN | CZC132BM61
WK302EA#ABN | CZC1331JR2
WK302EA#ABN | CZC1331JR3
WK302EA#ASZ | CZC132BM61
WK302EA#ASZ | CZC1331JR2
WK302EA#ASZ | CZC1331JR3
VY623AA#ABN|CN3129300D
VY623AA#ABN|CN3129300Z
VY623AA#荷兰银行| CN3129306S
VY623AA荷兰银行312930LM
VY623AS | 3129300X元
VY623AS | 3129300P元
一些简单的模板和适当的选择应该可以做到这一点。模板可以像函数一样具有参数
(对代码进行了测试,因此应能产生正确的输出)
让我用伪代码为您编写
你需要一条输出线
对于每个订单项
如果至少有一个选项,其中类型=#
对于类型=#
对于每个项目,请提供详细信息
一个输出元素序列号productNo+#+此选项/编号+|+此项目详细信息/编号
其他的
对于每个项目,请提供详细信息
一个输出元素序列号productNo+#+此项目发货详情/编号
用一些(.)和(…)填充它以获得最终输出 如果这是需要serialNO
项的顺序,则此转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="orderItems">
<orders>
<xsl:apply-templates select="orderItem/productNo"/>
</orders>
</xsl:template>
<!-- option(s) type '#' present: work with those as base -->
<xsl:template match="productNo[following-sibling::options/option/@type='#']">
<xsl:apply-templates select="following-sibling::options/option[@type='#']"/>
</xsl:template>
<!-- no options type '#' present: just process its serial numbers
(passing the product number) -->
<xsl:template match="productNo">
<xsl:apply-templates select="following-sibling::itemShipDetails/itemShipDetail/serialNumber">
<xsl:with-param name="productNoAndOptionNo"
select="."/>
</xsl:apply-templates>
</xsl:template>
<!-- option type '#': now process the serial numbers
(passing the product number and option number concatenation -->
<xsl:template match="option[@type='#']">
<xsl:apply-templates select="../preceding-sibling::itemShipDetails/itemShipDetail/serialNumber">
<xsl:with-param name="productNoAndOptionNo"
select="concat(../preceding-sibling::productNo, '#', number)"/>
</xsl:apply-templates>
</xsl:template>
<!-- output formatted serial number -->
<xsl:template match="serialNumber">
<xsl:param name="productNoAndOptionNo"/>
<serialNO>
<xsl:value-of select="concat($productNoAndOptionNo, '|', .)"/>
</serialNO>
</xsl:template>
</xsl:stylesheet>
应用于文档时,会产生以下结果:
<orders>
<serialNO>WK302EA#ABN|CZC132BM61</serialNO>
<serialNO>WK302EA#ABN|CZC1331JR2</serialNO>
<serialNO>WK302EA#ABN|CZC1331JR3</serialNO>
<serialNO>WK302EA#ASZ|CZC132BM61</serialNO>
<serialNO>WK302EA#ASZ|CZC1331JR2</serialNO>
<serialNO>WK302EA#ASZ|CZC1331JR3</serialNO>
<serialNO>VY623AA#ABN|CN3129300D</serialNO>
<serialNO>VY623AA#ABN|CN3129300Z</serialNO>
<serialNO>VY623AA#ABN|CN3129306S</serialNO>
<serialNO>VY623AA#ABN|CN312930LM</serialNO>
<serialNO>VY623AS|CN3129300X</serialNO>
<serialNO>VY623AS|CN3129300P</serialNO>
</orders>
WK302EA#ABN | CZC132BM61
WK302EA#ABN | CZC1331JR2
WK302EA#ABN | CZC1331JR3
WK302EA#ASZ | CZC132BM61
WK302EA#ASZ | CZC1331JR2
WK302EA#ASZ | CZC1331JR3
VY623AA#ABN|CN3129300D
VY623AA#ABN|CN3129300Z
VY623AA#荷兰银行| CN3129306S
VY623AA荷兰银行312930LM
VY623AS | 3129300X元
VY623AS | 3129300P元
这是我得到的:CZC132BM61CZC1331JR2CZC1331JR3CZC132BM61CZC1331JR2CZC1331JR3CN3129300DCN3129300ZCN3129300SCN312930LMCN3129300XCN3129300P
-请验证您的代码,修复SealNumber->serialNumber最后一个模板中的小错误。
<orders>
<serialNO>WK302EA#ABN|CZC132BM61</serialNO>
<serialNO>WK302EA#ABN|CZC1331JR2</serialNO>
<serialNO>WK302EA#ABN|CZC1331JR3</serialNO>
<serialNO>WK302EA#ASZ|CZC132BM61</serialNO>
<serialNO>WK302EA#ASZ|CZC1331JR2</serialNO>
<serialNO>WK302EA#ASZ|CZC1331JR3</serialNO>
<serialNO>VY623AA#ABN|CN3129300D</serialNO>
<serialNO>VY623AA#ABN|CN3129300Z</serialNO>
<serialNO>VY623AA#ABN|CN3129306S</serialNO>
<serialNO>VY623AA#ABN|CN312930LM</serialNO>
<serialNO>VY623AS|CN3129300X</serialNO>
<serialNO>VY623AS|CN3129300P</serialNO>
</orders>