0

我有以下示例 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>

预期的输出是:

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

3 回答 3

1

Some easy templates with proper selects should do it. 模板可以像函数一样具有参数。

(测试了代码,所以它应该产生正确的输出)

<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>
于 2012-12-09T09:19:34.063 回答
0

让我用伪代码为你写。

  如果至少有一个选项,则
 每个 orderItem 需要一个输出行 where type = #    For each options/option where type = #     For each itemShipDetail      一个输出元素 Serial No productNo + # + this-option/number + | + this-itemShipDetail/Number   else    对于每个 ItemShipDetail     一个输出元素 序列号 productNo + # + this-itemShipDetail/Number






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

于 2012-12-09T18:38:48.480 回答
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>
于 2012-12-09T18:57:01.450 回答