2012-12-09 58 views
0

我有下面的示例xml數據。該場景是當type =#時,productNo元素必須與類型元素值和數字元素值連接。串聯的輸出必須與該orderItem記錄中的每個serialNumber元素連接。用XSLT連接多個重複元素

最終要求是: 1.當type元素是'#'時,那麼每個type元素和數字元素的productNo級聯應與每個orderItem記錄中的每個serialNumber元素連接。 2.當類型元素沒有「#」然後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> 

回答

1

適當一些簡單的模板選擇應該這樣做。模板可以像函數一樣具有參數。

(測試的代碼,因此它應該產生正確的輸出)

<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> 
+0

代碼不是在所有 – user1658369

+0

這個工作是我所得到的:' CZC132BM61CZC1331JR2CZC1331JR3CZC132BM61CZC1331JR2CZC1331JR3CN3129300DCN3129300ZCN3129306SCN312930LMCN3129300XCN3129300P' - 請確認您的代碼 – mousio

+0

修復了來自searialNumber - > serialNumber的最後一個模板中的小錯字。 – Joep

0

讓我寫你的僞代碼。

您需要一個輸出線
 對於每個OrderItem的
   如果存在至少一個選項,其中類型=#
     對於每個選項/選項,其中類型=#
       對於每個itemShipDetail
           一個輸出元素序列號productNo +#+ this-option/number + | +此-itemShipDetail /號碼
   別的
     對於每個ItemShipDetail
       一個輸出元件序列號productNo +#+此-itemShipDetail /號碼

填充與某些(。) s和(..)s來獲得您的最終輸出。

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>