2017-06-14 115 views
0

我在使用xslt將以下源XML轉換爲目標XML。XSLT轉換

在源XML我有多個action標籤。 每action/action_kind/code='pickup'會有另外action/action_kind/code='deliver'

在這兩種action標籤orderId是一樣的。

我需要選擇基於此fromto標籤,我需要填充下圖的目標XMLas。

源XML

<section> 
 
    
 
    <action> 
 
    <action_kind> 
 
     <code>pickup</code> 
 
    </action_kind> 
 
    <pickup> 
 
     <orderId>11</orderId> 
 
    </pickup> 
 
    <name>NameOne</name> 
 
    <address>AddressOne</address> 
 
    </action> 
 
    
 
    <action> 
 
    <action_kind> 
 
     <code>pickup</code> 
 
    </action_kind> 
 
    <pickup> 
 
     <orderId>22</orderId> 
 
    </pickup> 
 
    <name>NameTwo</name> 
 
    <address>AddressTwo</address> 
 
    </action> 
 
    
 
    <action> 
 
    <action_kind> 
 
     <code>deliver</code> 
 
    </action_kind> 
 
    <expected> 
 
     <from>FirstOrderFro</from> 
 
     <to>FirstOrderTo</to> 
 
    </expected> 
 
    <delivery> 
 
     <orderId>11</orderId> 
 
    </delivery> 
 
    </action> 
 
    
 
    <action> 
 
    <action_kind> 
 
     <code>deliver</code> 
 
    </action_kind> 
 
    <expected> 
 
     <from>ScondOrderFro</from> 
 
     <to>SecondOrderTo</to> 
 
    </expected> 
 
    <delivery> 
 
     <orderId>22</orderId> 
 
    </delivery> 
 
    </action> 
 
    
 
</section>

目標XML

<Orders> 
 
    <Order> 
 
    <OrderId>11</OrderId> 
 
    <Name>NameOne</Name> 
 
    <Address>AddressOne</Address> 
 
    <From>FirstOrderFro</From> 
 
    <To>FirstOrderTo</To> 
 
    </Order> 
 
    <Order> 
 
    <OrderId>22</OrderId> 
 
    <Name>NameTwo</Name> 
 
    <Address>AddressTwo</Address> 
 
    <From>SecondOrderFro</From> 
 
    <To>SecondOrderTo</To> 
 
    </Order> 
 
</Orders>

有人能給我的xslt解決方案,它可以將源XML轉換爲目標XML?

+2

你忘了問一個問題,或者試圖自己解決問題,或者解釋你到目前爲止嘗試過哪些地方出了問題。 –

+0

我正在尋找將源XML轉換爲目標XML的xslt解決方案 – Jagadeesh

+2

我們不爲您編寫代碼,我們回答您的問題。你的問題是什麼? –

回答

0

嘗試了多種方法後,我發現下面的方式來獲得預期的輸出。

<xsl:template match="/"> 
 
    <Orders> 
 
    <xsl:for-each select="/section/action"> 
 
     <xsl:variable name="PickupOrderId" select="pickup/orderId"/> 
 
     <xsl:if test="action_kind/code='pickup'"> 
 
     <Order> 
 
      <OrderId> 
 
      <xsl:value-of select="pickup/orderId"/> 
 
      </OrderId> 
 
      <Name> 
 
      <xsl:value-of select="name"/> 
 
      </Name> 
 
      <Address> 
 
      <xsl:value-of select="address"/> 
 
      </Address> 
 
      <xsl:for-each select="/section/action"> 
 
      <xsl:if test="action_kind/code='deliver'"> 
 
       <xsl:if test="./delivery/orderId=$PickupOrderId"> 
 
       <From> 
 
        <xsl:value-of select="expected/from"/> 
 
       </From> 
 
       <To> 
 
        <xsl:value-of select="expected/to"/> 
 
       </To> 
 
       </xsl:if> 
 
      </xsl:if> 
 
      </xsl:for-each> 
 
     </Order> 
 
     </xsl:if> 
 
    </xsl:for-each> 
 
    </Orders> 
 
</xsl:template>

0

下面是使用Muenchian分組的就像我在我的評論中提到的另一種選擇。

XML輸入

<section> 
    <action> 
     <action_kind> 
      <code>pickup</code> 
     </action_kind> 
     <pickup> 
      <orderId>11</orderId> 
     </pickup> 
     <name>NameOne</name> 
     <address>AddressOne</address> 
    </action> 
    <action> 
     <action_kind> 
      <code>pickup</code> 
     </action_kind> 
     <pickup> 
      <orderId>22</orderId> 
     </pickup> 
     <name>NameTwo</name> 
     <address>AddressTwo</address> 
    </action> 
    <action> 
     <action_kind> 
      <code>deliver</code> 
     </action_kind> 
     <expected> 
      <from>FirstOrderFro</from> 
      <to>FirstOrderTo</to> 
     </expected> 
     <delivery> 
      <orderId>11</orderId> 
     </delivery> 
    </action> 
    <action> 
     <action_kind> 
      <code>deliver</code> 
     </action_kind> 
     <expected> 
      <from>ScondOrderFro</from> 
      <to>SecondOrderTo</to> 
     </expected> 
     <delivery> 
      <orderId>22</orderId> 
     </delivery> 
    </action> 
</section> 

XSLT 1.0

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

    <xsl:key name="actionByOrderId" match="action" use="*/orderId"/> 

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

    <xsl:template match="section"> 
    <Orders> 
     <xsl:apply-templates select="@*"/> 
     <xsl:for-each select="action[count(.|key('actionByOrderId',*/orderId)[1])=1]"> 
     <Order> 
      <OrderId><xsl:value-of select="*/orderId"/></OrderId> 
      <xsl:apply-templates select="key('actionByOrderId',*/orderId)"/>   
     </Order> 
     </xsl:for-each> 
    </Orders> 
    </xsl:template> 

    <xsl:template match="action"> 
    <xsl:apply-templates select="name|address|expected/*"/> 
    </xsl:template> 

</xsl:stylesheet> 

輸出

<Orders> 
    <Order> 
     <OrderId>11</OrderId> 
     <name>NameOne</name> 
     <address>AddressOne</address> 
     <from>FirstOrderFro</from> 
     <to>FirstOrderTo</to> 
    </Order> 
    <Order> 
     <OrderId>22</OrderId> 
     <name>NameTwo</name> 
     <address>AddressTwo</address> 
     <from>ScondOrderFro</from> 
     <to>SecondOrderTo</to> 
    </Order> 
</Orders> 

注:我離開了在p藝術資本化元素名稱的第一個字母。