需要有关XSLT逻辑的帮助

时间:2013-03-18 12:59:56

标签: xml xslt

任何人都可以帮助我在这里获取XSLT逻辑。我已经在输出中解释了需求作为注释。请找到下面的输出和输入。

输入:

    <OutputParameters>

  <OUT_SHIPMENT_TBL>
    <OUT_SHIPMENT_TBL_ITEM>
    <SLevel1>1</SLevel1>
    <SLevel1>S</SLevel1>
      <DELIVERY_ID>2334356776</DELIVERY_ID>
      <ORDER_SHIP_CODE_EXT>3467</ORDER_SHIP_CODE_EXT>
      <CUSTOMER_NUMBER>4838</CUSTOMER_NUMBER>
      <OEH_POSTAL_CODE  xsi:nil="true"/>
      <OEH_COUNTRY  xsi:nil="true"/>

      <ORDER_TBL_TYPE>
        <ORDER_TBL_TYPE_ITEM>
        <OLevel1>2333</OLevel1>
        <OLevel2>344</OLevel2>
        <OLevel3>O</OLevel3>
          <PURCHASE_ORDER_NUMBER>95678</PURCHASE_ORDER_NUMBER>
          <TOTAL_ORDER_GROSSWEIGHT>345</TOTAL_ORDER_GROSSWEIGHT>
          <TOTAL_ORDER_SHIPQTY>32</TOTAL_ORDER_SHIPQTY>

          <ORDER_LINES_TBL_TYPE>
            <ORDER_LINES_TBL_TYPE_ITEM>
              <!-- unbounded -->

              <ILevel1>2333</ILevel1>
              <ILevel2>2333</ILevel2>
              <ILevel3>2333</ILevel3>
              <PALLET_NUMBER>4546</PALLET_NUMBER>
              <CARTON_NUMBER>344235</CARTON_NUMBER>
              <WEB_TRANSACTION_ID>346788</WEB_TRANSACTION_ID>
              <FREIGHT_TERMS_CODE_INT>243546</FREIGHT_TERMS_CODE_INT>
              <VENDOR_NUM>344677</VENDOR_NUM>
              <ITEM>50116</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>

            <ORDER_LINES_TBL_TYPE_ITEM>
            <ILevel1>2333</ILevel1>
              <ILevel2>2333</ILevel2>
              <ILevel3>2333</ILevel3>
              <PALLET_NUMBER>4546</PALLET_NUMBER>
              <CARTON_NUMBER>344235</CARTON_NUMBER>
              <WEB_TRANSACTION_ID>346788</WEB_TRANSACTION_ID>
              <FREIGHT_TERMS_CODE_INT>243546</FREIGHT_TERMS_CODE_INT>
              <VENDOR_NUM>344677</VENDOR_NUM>
              <ITEM>50116</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>





          </ORDER_LINES_TBL_TYPE>
        </ORDER_TBL_TYPE_ITEM>

        <!-- 2 nd Order -->

      </ORDER_TBL_TYPE>
    </OUT_SHIPMENT_TBL_ITEM>
  </OUT_SHIPMENT_TBL>
</OutputParameters>

1)有多个<ORDER_TBL_TYPE_ITEM>

2)内部<ORDER_TBL_TYPE_ITEM>多个<ORDER_LINES_TBL_TYPE_ITEM>存在。

3)对于每次迭代,我们需要一个分配给下面显示元素的数字序列,如果previous = current忽略level1,2,3元素并且直接传递ITEM值,则条件也存在。

输出:

    <OutputParameters>

  <OUT_SHIPMENT_TBL>
    <OUT_SHIPMENT_TBL_ITEM>
    <SLevel1>1</SLevel1>
    <SLevel1>S</SLevel1>
      <DELIVERY_ID>3335</DELIVERY_ID>
      <ORDER_SHIP_CODE_EXT>445</ORDER_SHIP_CODE_EXT>
      <CUSTOMER_NUMBER>4838</CUSTOMER_NUMBER>
      <OEH_POSTAL_CODE  xsi:nil="true"/>
      <OEH_COUNTRY  xsi:nil="true"/>

      <ORDER_TBL_TYPE>
        <ORDER_TBL_TYPE_ITEM>
          <!-- unbounded -->
          <OLevel1>2</OLevel1>
        <OLevel2>1</OLevel2>
        <OLevel3>O</OLevel3>
          <PURCHASE_ORDER_NUMBER>44342</PURCHASE_ORDER_NUMBER>
          <!-- assign sequnce number starts from 2 -->
          <DELIVERY_ID>133232</DELIVERY_ID>
          <!-- hard code value as 1 -->
          <TOTAL_ORDER_GROSSWEIGHT>42323C</TOTAL_ORDER_GROSSWEIGHT>
          <!-- hard code value as C -->
          <TOTAL_ORDER_SHIPQTY>32</TOTAL_ORDER_SHIPQTY>

          <ORDER_LINES_TBL_TYPE>

              <!-- unbounded -->



            <!-- The UniqueID is the temporary variable.PALLET_NUMBER is coming from input.

if PALLET_NUMBER!=NULL then

UniqueID=substring (PALLET_NUMBER, 10)

if PALLET_NUMBER =NULL then

UniqueID=substring (CARTON_NUMBER, 7) -->

<!-- Condition here If previous UniqueID != current UniqueID  and PALLET_NUMBER!=NULL  then-->IF TRUE
<ORDER_LINES_TBL_TYPE_ITEM>

              <ILevel1>3</ILevel1>
               <!-- assign sequnce number starts from 3 -->
              <ILevel2>2</ILevel2>
               <!-- assign sequnce number of <Olevel1> -->
              <ILevel3>T</ILevel3>
              <!-- hard code value as T-->
              <PALLET_NUMBER>45463456788899432445</PALLET_NUMBER>
              <CARTON_NUMBER>344235</CARTON_NUMBER>
              <WEB_TRANSACTION_ID>3y4</WEB_TRANSACTION_ID>

              <FREIGHT_TERMS_CODE_INT>442</FREIGHT_TERMS_CODE_INT>

              <VENDOR_NUM>C4466I</VENDOR_NUM>

              <ITEM>50116</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>

            <!-- Condition here If previous UniqueID != current UniqueID  and PALLET_NUMBER =NULL  then-->iF TRUE
            <ORDER_LINES_TBL_TYPE_ITEM>
            <ILevel1>4</ILevel1>
               <!-- assign sequnce number increment -->
              <ILevel2>2</ILevel2>
               <!-- assign sequnce number of <Olevel1> -->
              <ILevel3>P</ILevel3>
              <!-- hard code value as T-->
              <PALLET_NUMBER></PALLET_NUMBER>
              <CARTON_NUMBER>344235</CARTON_NUMBER>
              <WEB_TRANSACTION_ID>3y4</WEB_TRANSACTION_ID>

              <FREIGHT_TERMS_CODE_INT>442</FREIGHT_TERMS_CODE_INT>

              <VENDOR_NUM>C4466I</VENDOR_NUM>

              <ITEM>50116</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>



            <ORDER_LINES_TBL_TYPE_ITEM>
            <ILevel1>5</ILevel1>
               <!-- assign sequnce number increment -->
              <ILevel2>4</ILevel2>
               <!-- if T Level is true then <ILevel2> of T level 
               if P Level is true then <ILevel2> of P level 
               -->
              <ILevel3>I</ILevel3>
              <!-- hard code value as I-->
              <PALLET_NUMBER></PALLET_NUMBER>
              <CARTON_NUMBER>344235</CARTON_NUMBER>
              <WEB_TRANSACTION_ID>3y4</WEB_TRANSACTION_ID>

              <FREIGHT_TERMS_CODE_INT>442</FREIGHT_TERMS_CODE_INT>

              <VENDOR_NUM>C4466I</VENDOR_NUM>

              <ITEM>50116</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>



<!-- Condition here If previous UniqueID != current UniqueID  and PALLET_NUMBER!=NULL  then-->IF FALSE

            < NO LOOP HERE FOR T LEVEL>



            <!-- Condition here If previous UniqueID != current UniqueID  and PALLET_NUMBER =NULL  then-->iF TRUE
            <ORDER_LINES_TBL_TYPE_ITEM>
            <ILevel1>6</ILevel1>
              <ILevel2>2</ILevel2>
              <ILevel3>P</ILevel3>
              <WEB_TRANSACTION_ID>685858</WEB_TRANSACTION_ID>
              <FREIGHT_TERMS_CODE_INT>87899</FREIGHT_TERMS_CODE_INT>
              <VENDOR_NUM>457482</VENDOR_NUM>
              <ITEM>50119</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>


            <ORDER_LINES_TBL_TYPE_ITEM>
            <ILevel1>7</ILevel1>
              <ILevel2>6</ILevel2>
              <ILevel3>I</ILevel3>
              <WEB_TRANSACTION_ID>685858</WEB_TRANSACTION_ID>
              <FREIGHT_TERMS_CODE_INT>87899</FREIGHT_TERMS_CODE_INT>
              <VENDOR_NUM>457482</VENDOR_NUM>
              <ITEM>50119</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>

</ORDER_LINES_TBL_TYPE>

        </ORDER_TBL_TYPE_ITEM>

        <!-- 2 nd Order -->


      </ORDER_TBL_TYPE>
    </OUT_SHIPMENT_TBL_ITEM>
  </OUT_SHIPMENT_TB

L&GT;

2 个答案:

答案 0 :(得分:0)

您正在寻找的条件逻辑如下所示。我假设上下文节点是<colorItems>元素。

<xsl:if test="color != preceding-sibling::colorItems[1]/color">
    ... process Level1, 2, 3 ...
</xsl:if>
<xsl:copy-of select="color" />

答案 1 :(得分:0)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*" />

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="OUT_SHIPMENT_TBL_ITEM | 
                       ORDER_TBL_TYPE_ITEM[1] | 
                       ORDER_LINES_TBL_TYPE_ITEM[1]">
    <xsl:param name="currentSequenceNo" select="0" />

    <xsl:apply-templates select="." mode="iterate">
      <xsl:with-param name="currentSequenceNo" select="$currentSequenceNo" />
      <xsl:with-param name="parentSequenceNo" select="$currentSequenceNo" />
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="ORDER_TBL_TYPE_ITEM | ORDER_LINES_TBL_TYPE_ITEM" />

  <xsl:template match="*" mode="iterate">
    <xsl:param name="currentSequenceNo" />
    <xsl:param name="parentSequenceNo" />

    <xsl:variable name="differentFromPreviousStr">
      <xsl:apply-templates select="." mode="different" />
    </xsl:variable>
    <xsl:variable name="differentFromPrevious"
                  select="$differentFromPreviousStr = 'true'" />
    <xsl:variable name="thisSequenceNo"
                  select="$currentSequenceNo + $differentFromPrevious" />
    <xsl:copy>
      <xsl:apply-templates select="*[$differentFromPrevious or position() &gt; 3]">
        <xsl:with-param name="currentSequenceNo" select="$thisSequenceNo" />
        <xsl:with-param name="parentSequenceNo" select="$parentSequenceNo" />
        <xsl:with-param name="code">
          <xsl:apply-templates select="." mode="code" />
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:copy>

    <xsl:variable name="childSequenceCount">
      <xsl:apply-templates select="." mode="childSequence" />
    </xsl:variable>

    <xsl:apply-templates select="following-sibling::*[1]
                                     [name() = name(current())]"
                         mode="iterate">
      <xsl:with-param name="currentSequenceNo"
                      select="$thisSequenceNo + $childSequenceCount" />
      <xsl:with-param name="parentSequenceNo" select="$parentSequenceNo" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="*" mode="different">
    <xsl:text>true</xsl:text>
  </xsl:template>
  <xsl:template match="ORDER_LINES_TBL_TYPE_ITEM" mode="different">
    <xsl:value-of
         select="not(ITEM = preceding-sibling::ORDER_LINES_TBL_TYPE_ITEM[1]/ITEM)" />
  </xsl:template>

  <xsl:template match="*" mode="childSequence">
    <xsl:text>0</xsl:text>
  </xsl:template>
  <xsl:template match="ORDER_TBL_TYPE_ITEM" mode="childSequence">
    <xsl:value-of
       select="count(ORDER_LINES_TBL_TYPE/ORDER_LINES_TBL_TYPE_ITEM[not(
                              ITEM = 
                               preceding-sibling::ORDER_LINES_TBL_TYPE_ITEM[1]/ITEM
                                            )])" />
  </xsl:template>

  <xsl:template match="OUT_SHIPMENT_TBL_ITEM" mode="code">
    <xsl:text>E</xsl:text>
  </xsl:template>
  <xsl:template match="ORDER_TBL_TYPE_ITEM" mode="code">
    <xsl:text>C</xsl:text>
  </xsl:template>
  <xsl:template match="ORDER_LINES_TBL_TYPE_ITEM" mode="code">
    <xsl:text>CI</xsl:text>
  </xsl:template>

  <xsl:template match="OUT_SHIPMENT_TBL | ORDER_TBL_TYPE | ORDER_LINES_TBL_TYPE">
    <xsl:param name="currentSequenceNo" select="0" />
    <xsl:param name="parentSequenceNo" />

    <xsl:copy>
      <xsl:apply-templates>
        <xsl:with-param name ="currentSequenceNo" select="$currentSequenceNo" />
        <xsl:with-param name="parentSequenceNo" select="$parentSequenceNo" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="node()[self::OUT_SHIPMENT_TBL_ITEM or 
                              self::ORDER_TBL_TYPE_ITEM or
                              self::ORDER_LINES_TBL_TYPE_ITEM]/*[1]">
    <xsl:param name="currentSequenceNo" />
    <xsl:copy>
      <xsl:value-of select="$currentSequenceNo"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="node()[self::ORDER_TBL_TYPE_ITEM or
                              self::ORDER_LINES_TBL_TYPE_ITEM]/*[2]">
    <xsl:param name="parentSequenceNo" />
    <xsl:copy>
      <xsl:value-of select="$parentSequenceNo" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="OUT_SHIPMENT_TBL_ITEM/*[2] | 
                       node()[self::ORDER_TBL_TYPE_ITEM or
                              self::ORDER_LINES_TBL_TYPE_ITEM]/*[3]">
    <xsl:param name="code" />
    <xsl:copy>
      <xsl:value-of select="$code" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

在样本输入上运行时,会产生:

<OutputParameters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <OUT_SHIPMENT_TBL>
    <OUT_SHIPMENT_TBL_ITEM>
      <DELIVERY_ID>1</DELIVERY_ID>
      <ORDER_SHIP_CODE_EXT>E</ORDER_SHIP_CODE_EXT>
      <CUSTOMER_NUMBER>4838</CUSTOMER_NUMBER>
      <OEH_POSTAL_CODE xsi:nil="true" />
      <OEH_COUNTRY xsi:nil="true" />
      <ORDER_TBL_TYPE>
        <ORDER_TBL_TYPE_ITEM>
          <PURCHASE_ORDER_NUMBER>2</PURCHASE_ORDER_NUMBER>
          <TOTAL_ORDER_GROSSWEIGHT>1</TOTAL_ORDER_GROSSWEIGHT>
          <TOTAL_ORDER_SHIPQTY>C</TOTAL_ORDER_SHIPQTY>
          <ORDER_LINES_TBL_TYPE>
            <ORDER_LINES_TBL_TYPE_ITEM>
              <WEB_TRANSACTION_ID>3</WEB_TRANSACTION_ID>
              <FREIGHT_TERMS_CODE_INT>2</FREIGHT_TERMS_CODE_INT>
              <VENDOR_NUM>CI</VENDOR_NUM>
              <ITEM>50116</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>
            <ORDER_LINES_TBL_TYPE_ITEM>
              <ITEM>50116</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>
            <ORDER_LINES_TBL_TYPE_ITEM>
              <WEB_TRANSACTION_ID>4</WEB_TRANSACTION_ID>
              <FREIGHT_TERMS_CODE_INT>2</FREIGHT_TERMS_CODE_INT>
              <VENDOR_NUM>CI</VENDOR_NUM>
              <ITEM>6435</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>
          </ORDER_LINES_TBL_TYPE>
        </ORDER_TBL_TYPE_ITEM>
        <ORDER_TBL_TYPE_ITEM>
          <PURCHASE_ORDER_NUMBER>5</PURCHASE_ORDER_NUMBER>
          <DELIVERY_ID>1</DELIVERY_ID>
          <TOTAL_ORDER_GROSSWEIGHT>C</TOTAL_ORDER_GROSSWEIGHT>
          <TOTAL_ORDER_SHIPQTY>32</TOTAL_ORDER_SHIPQTY>
          <ORDER_LINES_TBL_TYPE>
            <ORDER_LINES_TBL_TYPE_ITEM>
              <WEB_TRANSACTION_ID>6</WEB_TRANSACTION_ID>
              <FREIGHT_TERMS_CODE_INT>5</FREIGHT_TERMS_CODE_INT>
              <VENDOR_NUM>CI</VENDOR_NUM>
              <ITEM>50119</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>
            <ORDER_LINES_TBL_TYPE_ITEM>
              <WEB_TRANSACTION_ID>7</WEB_TRANSACTION_ID>
              <FREIGHT_TERMS_CODE_INT>5</FREIGHT_TERMS_CODE_INT>
              <VENDOR_NUM>CI</VENDOR_NUM>
              <ITEM>87554</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>
            <ORDER_LINES_TBL_TYPE_ITEM>
              <ITEM>87554</ITEM>
              <CROSS_REFERENCE_UPC>034138102008</CROSS_REFERENCE_UPC>
            </ORDER_LINES_TBL_TYPE_ITEM>
          </ORDER_LINES_TBL_TYPE>
        </ORDER_TBL_TYPE_ITEM>
        <!-- 2 nd Order -->
      </ORDER_TBL_TYPE>
    </OUT_SHIPMENT_TBL_ITEM>
  </OUT_SHIPMENT_TBL>
</OutputParameters>