xslt for-each对所有节点重复相同

时间:2016-04-25 03:12:07

标签: xml xslt nodes repeat

源XML: 在xslt中使用的for-each中重复相同的节点。我需要循环遍历所有订单项。如果我有3个订单项,则使用相同的第一个订单项重复3次。

        <?xml version="1.0" encoding="UTF-8"?>
        <orders xmlns="http://www.oracle.com">
                    <product-lineitems>
                    <product-lineitem>
                        <net-price>100.00</net-price>
                        <tax>6.35</tax>
                        <gross-price>106.35</gross-price>
                        <base-price>100</base-price>
                        <lineitem-text>Originals Modern</lineitem-text>
                        <tax-basis>100.00</tax-basis>
                        <product-id>TW2P84400ZA</product-id>
                        <product-name>The Waterbury</product-name>
                        <quantity unit="">1.0</quantity>
                        <tax-rate>0.0635</tax-rate>
                        <shipment-id>1234</shipment-id>
                    </product-lineitem>
                    <product-lineitem>
                        <net-price>130.00</net-price>
                        <tax>8.26</tax>
                        <gross-price>138.26</gross-price>
                        <base-price>65.00</base-price>
                        <lineitem-text>Weekender Slip-thru</lineitem-text>
                        <tax-basis>130.00</tax-basis>
                        <product-id>TW2P919009J</product-id>
                        <product-name>Weekender Fairfield65</product-name>
                        <quantity unit="">2.0</quantity>
                        <tax-rate>0.0635</tax-rate>
                        <shipment-id>1234</shipment-id>
                        <price-adjustments>
                            <price-adjustment>
                                <net-price>-26.00</net-price>
                                <tax>-1.65</tax>
                                <gross-price>-27.65</gross-price>
                                <base-price>-26.00</base-price>
                                <tax-basis>-26.00</tax-basis>
                                <promotion-id>fathers_day_20</promotion-id>
                                <campaign-id>FATHER'S DAY 20% OFF - 04/01/14 9:22:15 am</campaign-id>
                                <coupon-id>FATHER20</coupon-id>
                            </price-adjustment>
                        </price-adjustments>
                    </product-lineitem>
                    <product-lineitem>
                        <net-price>60.00</net-price>
                        <tax>3.81</tax>
                        <gross-price>63.81</gross-price>
                        <base-price>20.00</base-price>
                        <lineitem-text>Straps - Weekender</lineitem-text>
                        <tax-basis>60.00</tax-basis>
                        <product-id>TW7C06900GZ</product-id>
                        <product-name>Weekender™ Fairfield Red/White/Blue Strap, 20mm</product-name>
                        <quantity unit="">3.0</quantity>
                        <tax-rate>0.0635</tax-rate>
                        <shipment-id>1234</shipment-id>
                        <price-adjustments>
                            <price-adjustment>
                                <net-price>-12.00</net-price>
                                <tax>-0.76</tax>
                                <gross-price>-12.76</gross-price>
                                <base-price>-12.00</base-price>
                                <tax-basis>-12.00</tax-basis>
                                <promotion-id>fathers_day_20</promotion-id>
                                <campaign-id>FATHER'S DAY 20% OFF - 04/01/14 9:22:15 am</campaign-id>
                                <coupon-id>FATHER20</coupon-id>
                            </price-adjustment>
                        </price-adjustments>
                    </product-lineitem>
                </product-lineitems>
                <shipping-lineitems>
                    <shipping-lineitem>
                        <net-price>12.95</net-price>
                        <tax>0.82</tax>
                        <gross-price>13.77</gross-price>
                        <base-price>12.95</base-price>
                        <lineitem-text>Shipping</lineitem-text>
                        <tax-basis>12.95</tax-basis>
                        <price-adjustment>
                            <net-price>-7.95</net-price>
                            <tax>-0.50</tax>
                            <gross-price>-8.45</gross-price>
                            <base-price>-7.95</base-price>
                            <tax-basis>-7.95</tax-basis>
                            <lineitem-text>$5 Expedited Shipping</lineitem-text>
                            <promotion-id>fathers_day_shipping</promotion-id>
                            <campaign-id>FATHER'S DAY $5 Shipping - 04/01/14 9:22:15 am</campaign-id>
                            <coupon-id>FATHER20</coupon-id>
                        </price-adjustment>
                        <item-id>EXPEDITED_SHIPPING</item-id>
                        <shipment-id>1234</shipment-id>
                        <tax-rate>.0635</tax-rate>
                    </shipping-lineitem>
                </shipping-lineitems>
                <shipments>
                    <shipment>
                        <shipping-method>EXPEDITED-SHIPPING</shipping-method>
                        <shipping-address>
                            <first-name>Satish</first-name>
                            <last-name>Kumar</last-name>
                            <address1>555 Christian Rd</address1>
                            <address2>Middlebury</address2>
                            <city>CT</city>
                            <postal-code>06762</postal-code>
                            <state-code>CT</state-code>
                            <country-code>US</country-code>
                            <phone>2033461234</phone>
                        </shipping-address>
                        <shipment-id></shipment-id>
                    </shipment>
                </shipments>
                <totals>
                    <merchandize-total>
                        <net-price>290.00</net-price>
                        <tax>18.42</tax>
                        <gross-price>308.42</gross-price>
                    </merchandize-total>
                    <adjusted-merchandize-total>
                        <net-price>252.00</net-price>
                        <tax>16.00</tax>
                        <gross-price>268.83</gross-price>
                    </adjusted-merchandize-total>
                    <shipping-total>
                        <net-price>12.95</net-price>
                        <tax>0.82</tax>
                        <gross-price>13.77</gross-price>
                    </shipping-total>
                    <adjusted-shipping-total>
                        <net-price>5.00</net-price>
                        <tax>0.32</tax>
                        <gross-price>5.32</gross-price>
                    </adjusted-shipping-total>
                    <order-total>
                        <net-price>257.00</net-price>
                        <tax>16.32</tax>
                        <gross-price>273.32</gross-price>
                    </order-total>
                </totals>
                <payments>
                    <payment>
                        <credit-card>
                            <card-type>VISA</card-type>
                            <card-token>AH92FU288BS</card-token>
                            <card-holder>Jon Paul</card-holder>
                            <expiration-month>1</expiration-month>
                            <expiration-year>2017</expiration-year>
                        </credit-card>
                        <amount>185.05</amount>
                        <transaction-id>XYZ123</transaction-id>
                        <processor-id>MYINFO</processor-id>
                        <transaction-type>AUTH</transaction-type>
                        <orderBusinessType>B2C</orderBusinessType>
                    </payment>
                </payments>
            </order>
        </orders>   


    XSLT USED:

    <template match="/">
          <!--HEADERS LEVEL MAPPING FOR THE ORDER IS WORKING FINE. I AM HAVING A PROBLEM WITH THE LINES MAPPING.-->
        </XxtgEcomOmsHeadersAll>
        <xxtgEcomOmsLinesAllCollection>
          <for-each select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem">
            <XxtgEcomOmsLinesAll>

              <tmxFwp>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:product-id"/>
              </tmxFwp>
              <qtyOrdered>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:quantity"/>
              </qtyOrdered>
              <lineDescription>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:product-name"/>
              </lineDescription>
              <lineStatusCode>
                <value-of select="/imp1:orders/imp1:order/imp1:status/imp1:order-status"/>
              </lineStatusCode>
              <omsLinePrice>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:net-price"/>
              </omsLinePrice>
              <omsLineTax>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:tax"/>
              </omsLineTax>
              <omsLineGrossPrice>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:gross-price"/>
              </omsLineGrossPrice>
              <omsLineBasePrice>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:base-price"/>
              </omsLineBasePrice>
              <omsLineItemText>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:lineitem-text"/>
              </omsLineItemText>
              <omsLineTaxBasis>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:tax-basis"/>
              </omsLineTaxBasis>
              <omsLineTaxRate>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:tax-rate"/>
              </omsLineTaxRate>
              <omsLineNetPriceAdj>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:price-adjustments/imp1:price-adjustment/imp1:net-price"/>
              </omsLineNetPriceAdj>
              <omsLineTaxAdj>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:price-adjustments/imp1:price-adjustment/imp1:tax"/>
              </omsLineTaxAdj>
              <omsLineGrossPriceAdj>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:price-adjustments/imp1:price-adjustment/imp1:gross-price"/>
              </omsLineGrossPriceAdj>
              <omsLineBasePriceAdj>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:price-adjustments/imp1:price-adjustment/imp1:base-price"/>
              </omsLineBasePriceAdj>
              <omsLineTaxBasisAdj>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:price-adjustments/imp1:price-adjustment/imp1:tax-basis"/>
              </omsLineTaxBasisAdj>
              <omsLinePromotionId>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:price-adjustments/imp1:price-adjustment/imp1:promotion-id"/>
              </omsLinePromotionId>
              <omsLineCompaignId>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:price-adjustments/imp1:price-adjustment/imp1:campaign-id"/>
              </omsLineCompaignId>
              <omsLineCouponId>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:price-adjustments/imp1:price-adjustment/imp1:coupon-id"/>
              </omsLineCouponId>
              <lineUnitPrice>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:base-price"/>
              </lineUnitPrice>
              <lineUnitCost>
                <value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:base-price"/>
              </lineUnitCost>
               </XxtgEcomOmsLinesAll>
          </for-each>

    </template>

Unable to do the for-each for all the nodes. That is the issue.


In out put i see first line item TW2P84400ZA repeated 3 times.
**>TW2P84400ZA<
>TW2P84400ZA<
>TW2P84400ZA<**    

Instead i need to see all the line items in the output
**>TW2P84400ZA<
>TW2P919009J<
>TW7C06900GZ<**

由于

1 个答案:

答案 0 :(得分:0)

简而言之,问题是:

当你这样做时:

<xsl:for-each select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem">

你被置于product-lineitem的背景中。在此上下文中,您只需指定产品ID的相对路径:

<xsl:value-of select="imp1:product-id"/>

以选择当前的订单项“product-id

相反,你这样做:

<xsl:value-of select="/imp1:orders/imp1:order/imp1:product-lineitems/imp1:product-lineitem/imp1:product-id"/>

从根目录开始选择整个文档中的所有product-id个节点,并在 - XSLT 1.0中 - 返回第一个节点的值。

相关问题