使用XSLT连接多个重复元素

时间:2012-12-09 07:21:32

标签: xml xslt tibco

我有以下示例xml数据。方案是当type =#时,productNo元素必须与类型元素值和数字元素值连接在一起。连接的输出必须与该orderItem记录中的每个serialNumber元素连接。

最终要求是: 1.当type元素为'#'时,productNo与每个type元素和数字元素的连接应该与每个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>

预期的输出是:

    <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>

3 个答案:

答案 0 :(得分:1)

一些具有正确选择的简单模板应该这样做。模板可以具有与函数类似的参数。

(测试代码,因此它应该产生正确的输出)

<xsl:template match="orderItems">
    <orders>
        <xsl:apply-templates select="orderItem/options/option[@type != '']"/>
    </orders>
</xsl:template>

<xsl:template match="option[@type = '#']">
    <xsl:apply-templates select="../../itemShipDetails/itemShipDetail/serialNumber">
        <xsl:with-param name="productNo" select="concat(../../productNo, '#', number)"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="option[@type != '#']">
    <xsl:apply-templates select="../../itemShipDetails/itemShipDetail/serialNumber">
        <xsl:with-param name="productNo" select="../../productNo"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="serialNumber">
    <xsl:param name="productNo"/>
    <serialNO><xsl:value-of select="concat($productNo, '|', .)"/></serialNO>
</xsl:template>

答案 1 :(得分:0)

让我用伪代码为你写。

您需要一条输出线
对于每个orderItem
如果至少有一个选项,其中type =#
对于type =#的每个选项/选项
对于每个itemShipDetail
一个输出元素Serial No productNo +#+ this-option / number + | + this-itemShipDetail / Number
否则
对于每个ItemShipDetail
一个输出元素Serial No productNo +#+ this-itemShipDetail / Number

用一些(。)和(..)s填充它以获得最终输出。

答案 2 :(得分:0)

如果这是需要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>