2014-01-20 40 views
0

我有以下的XML和要複製<itemInformation>元素低於<orderLineItem>元素:複製子節點與匹配位置號碼

<?xml version="1.0" encoding="UTF-8"?> 
<Envelope> 
<order> 
    <orderLineItem>1 
     <requestedQuantity> 
      <value>2</value> 
     </requestedQuantity> 
     <tradeItemIdentification> 
      <gtin>07610400013192</gtin> 
     </tradeItemIdentification> 
    </orderLineItem> 
    <orderLineItem>2 
     <requestedQuantity> 
      <value>1</value> 
     </requestedQuantity> 
     <tradeItemIdentification> 
      <gtin>07610400014632</gtin> 
     </tradeItemIdentification> 
    </orderLineItem> 
    <extension> 
     <orderExtension> 
      <itemInformation>1 
       <tradeItemIdentification> 
        <gtin>07610400013192</gtin> 
       </tradeItemIdentification> 
       <grossCost> 
        <currencyCode> 
         <currencyISOCode>ZAR</currencyISOCode> 
        </currencyCode> 
        <monetaryAmount>243.60</monetaryAmount> 
       </grossCost> 
      </itemInformation> 
      <itemInformation>2 
       <tradeItemIdentification> 
        <gtin>07610400014632</gtin> 
       </tradeItemIdentification> 
       <grossCost> 
        <currencyCode> 
         <currencyISOCode>ZAR</currencyISOCode> 
        </currencyCode> 
        <monetaryAmount>243.60</monetaryAmount> 
       </grossCost> 
      </itemInformation> 
     </orderExtension> 
    </extension> 
</order> 
</Envelope> 

我想出了以下XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0"> 

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

<xsl:template match="order/orderLineItem"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*" /> 

     <xxxx> 
      <xsl:apply-templates select="parent::order/extension/orderExtension/itemInformation 
       [normalize-space(text())=normalize-space(ancestor::order/orderLineItem/text())]"/> 
     </xxxx> 
     <xsl:apply-templates select="node()"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

我預計這一目標XML:

<?xml version="1.0" encoding="UTF-8"?> 
<Envelope> 
<order> 
    <orderLineItem> 
     <xxxx> 
      <itemInformation>1 
       <tradeItemIdentification> 
        <gtin>07610400013192</gtin> 
       </tradeItemIdentification> 
       <grossCost> 
        <currencyCode> 
         <currencyISOCode>ZAR</currencyISOCode> 
        </currencyCode> 
        <monetaryAmount>243.60</monetaryAmount> 
       </grossCost> 
      </itemInformation> 
     </xxxx>1 
     <requestedQuantity> 
      <value>2</value> 
     </requestedQuantity> 
     <tradeItemIdentification> 
      <gtin>07610400013192</gtin> 
     </tradeItemIdentification> 
    </orderLineItem> 
    <orderLineItem> 
     <xxxx> 
      <itemInformation>2 
       <tradeItemIdentification> 
        <gtin>07610400014632</gtin> 
       </tradeItemIdentification> 
       <grossCost> 
        <currencyCode> 
         <currencyISOCode>ZAR</currencyISOCode> 
        </currencyCode> 
        <monetaryAmount>243.60</monetaryAmount> 
       </grossCost> 
      </itemInformation> 
     </xxxx>2 
     <requestedQuantity> 
      <value>1</value> 
     </requestedQuantity> 
     <tradeItemIdentification> 
      <gtin>07610400014632</gtin> 
     </tradeItemIdentification> 
    </orderLineItem> 
    <extension> 
     <orderExtension> 
      <itemInformation>1 
       <tradeItemIdentification> 
        <gtin>07610400013192</gtin> 
       </tradeItemIdentification> 
       <grossCost> 
        <currencyCode> 
         <currencyISOCode>ZAR</currencyISOCode> 
        </currencyCode> 
        <monetaryAmount>243.60</monetaryAmount> 
       </grossCost> 
      </itemInformation> 
      <itemInformation>2 
       <tradeItemIdentification> 
        <gtin>07610400014632</gtin> 
       </tradeItemIdentification> 
       <grossCost> 
        <currencyCode> 
         <currencyISOCode>ZAR</currencyISOCode> 
        </currencyCode> 
        <monetaryAmount>243.60</monetaryAmount> 
       </grossCost> 
      </itemInformation> 
     </orderExtension> 
    </extension> 
</order> 
</Envelope> 

但在第二<orderLineItem>元素我得到的第一個<itemInformation>而不是第二個。 所以我必須匹配<itemInformation> 1,2,...與<orderLineItem> 1,2,...我只是無法弄清楚我需要使用的XPATH。

回答

1

正確的XPath表達式如下:

../extension/orderExtension/itemInformation[normalize-space(text())=normalize-space(current()/text())] 

與你的問題是,你是搞亂與上下文節點,請記住謂詞(任何相對路徑表達你的把表達式括號)是指當前元素,即在表達式計算結果爲真時將被選中的元素。 current函數由XSLT定義,並引用它的上下文元素,它是整個XPath的相同上下文元素(但它在謂詞中發生變化)。

我也換成了parent::order一步,其縮寫語法..

+0

卡羅您好,非常感謝您 - 它完美的作品! (+1)我現在看到它,謝謝你的解釋。我的謂詞的第二部分是錯誤的(我很接近但看不到錯誤)。最好的問候,彼得 – Peter