2013-01-04 370 views
0

我需要XSLT 1.0中的幫助將以下源XML轉換爲結果XML。不太熟悉XSLT,並且同時在這個論壇中搜索有用的帖子。謝謝你的幫助。在XML轉換中需要XSLT幫助

源XML:

<BomCollection> 
    <BomAssyV> 
    <sender>Oracle</sender> 
    <messageid>ABCD1234EFG</messageid> 
    <bomid>1234</bomid> 
    <partid>4567</partid> 
    <loc>DEL</loc> 
    <country>IND</country> 
    <cost>30</cost> 
    <linenumber>1</linenumber> 
    <componentpartid>CID1</componentpartid> 
    <componenttype>CTYPE</componenttype> 
    <quantityperpart>2</quantityperpart> 
    </BomAssyV> 
    <BomAssyV> 
    <sender>Oracle</sender> 
    <messageid>ABCD1234EFG</messageid> 
    <bomid>1234</bomid> 
    <partid>4567</partid> 
    <loc>DEL</loc> 
    <country>IND</country> 
    <cost>30</cost> 
    <linenumber>2</linenumber> 
    <componentpartid>CID2</componentpartid> 
    <componenttype>CTYPE</componenttype> 
    <quantityperpart>30</quantityperpart> 
    </BomAssyV> 
    <BomAssyV> 
    <sender>Oracle</sender> 
    <messageid>ABCD1236EFG</messageid> 
    <bomid>4321</bomid> 
    <partid>8901</partid> 
    <loc>MUM</loc> 
    <country>IND</country> 
    <cost>45000</cost> 
    <linenumber>1</linenumber> 
    <componentpartid>PID3</componentpartid> 
    <componenttype>PTYPE</componenttype> 
    <quantityperpart28></quantityperpart28> 
    </BomAssyV> 
</BomCollection> 

結果XML:

<request> 
    <sender>Oracle</sender> 
    <messageId>ABCD1234EFG</messageId> 
    <header>  
    <bomid>1234</bomid> 
    <partId>4567</partId> 
    <loc>DEL</loc> 
    <country>IND</country> 
    <cost>30</cost> 
    <line> 
     <lineNumber>1</lineNumber> 
     <componentPartId>CID1</componentPartId>  
     <componentType>CTYPE</componentType>  
     <quantityPerPart>2</quantityPerPart>  
    </line> 
    <line> 
     <lineNumber>2</lineNumber> 
     <componentPartId>CID2</componentPartId>  
     <componentType>CTYPE</componentType>  
     <quantityPerPart>30</quantityPerPart>  
    </line> 
    </header> 
    <header>  
    <bomid>4321</bomid> 
    <partId>8901</partId> 
    <loc>MUM</loc> 
    <country>IND</country> 
    <cost>45000</cost> 
    <line> 
     <lineNumber>1</lineNumber> 
     <componentPartId>PID3</componentPartId>  
     <componentType>PTYPE</componentType>  
     <quantityPerPart></quantityPerPart>  
    </line> 
    </header> 
</request> 
+3

這個問題是一個位VA GUE。如果您在XSLT中尋找教程,那麼Google可能是您的朋友。如果你有一些更具體的東西,我們很樂意提供幫助。 – Will

+0

閱讀w3c教程。但需要上述要求的幫助。需要將我的文章中給出的Source XML轉換爲Result XML。 – user1948230

+3

這是一個非常直接的轉變。請提供您當前的XSLT模板和具體問題,而不是僅僅讓人們爲您編寫整個事件。 –

回答

0

你需要做一些分組 - 看看這個SO帖子:Applying Muenchian grouping for a simple XML with XSLT。具體來說,看看參考Jeni Tennison's Muenchain grouping guide

我在這裏假定只有一個request輸出元素(否則你將需要分組的第二級),同時又有bomidpartid綁。

的XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 

    <xsl:key name="bomids" match="BomAssyV" use="bomid" /> 

    <xsl:template match="BomCollection"> 
    <xsl:element name="request"> 
     <!-- We just scrape the first BomAssyV/sender and messageid elements --> 
     <sender> 
     <xsl:value-of select="BomAssyV/sender"/> 
     </sender> 
     <messageid> 
     <xsl:value-of select="BomAssyV/messageid"/> 
     </messageid> 
     <!--i.e. Only the first bom in every group--> 
     <xsl:apply-templates select="BomAssyV[generate-id()=generate-id(key('bomids', bomid)[1])]" mode="group"/> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="BomAssyV" mode="group"> 
    <header> 
     <bomid> 
     <xsl:value-of select="bomid"/> 
     </bomid> 
     <bomid> 
     <xsl:value-of select="partid"/> 
     </bomid> 
     <loc> 
     <xsl:value-of select="loc"/> 
     </loc> 
     <country> 
     <xsl:value-of select="country"/> 
     </country> 
     <cost> 
     <xsl:value-of select="cost"/> 
     </cost> 

     <xsl:for-each select="key('bomids', bomid)"> 
     <line> 
      <lineNumber> 
      <xsl:value-of select="linenumber"/> 
      </lineNumber> 
      <componentPartId> 
      <xsl:value-of select="componentpartid"/> 
      </componentPartId> 
      <componentType> 
      <xsl:value-of select="componenttype"/> 
      </componentType> 
      <quantityPerPart> 
      <xsl:value-of select="quantityperpart"/> 
      </quantityPerPart> 
     </line> 
     </xsl:for-each> 
    </header> 
    </xsl:template> 

</xsl:stylesheet> 

變換此輸入XML

<BomCollection> 
    <BomAssyV> 
    <sender>Oracle</sender> 
    <messageid>ABCD1234EFG</messageid> 
    <bomid>1234</bomid> 
    <partid>4567</partid> 
    <loc>DEL</loc> 
    <country>IND</country> 
    <cost>30</cost> 
    <linenumber>1</linenumber> 
    <componentpartid>CID1</componentpartid> 
    <componenttype>CTYPE</componenttype> 
    <quantityperpart>2</quantityperpart> 
    </BomAssyV> 
    <BomAssyV> 
    <sender>Oracle</sender> 
    <messageid>ABCD1234EFG</messageid> 
    <bomid>1234</bomid> 
    <partid>4567</partid> 
    <loc>DEL</loc> 
    <country>IND</country> 
    <cost>30</cost> 
    <linenumber>2</linenumber> 
    <componentpartid>CID2</componentpartid> 
    <componenttype>CTYPE</componenttype> 
    <quantityperpart>30</quantityperpart> 
    </BomAssyV> 
    <BomAssyV> 
    <sender>Oracle</sender> 
    <messageid>ABCD1236EFG</messageid> 
    <bomid>4321</bomid> 
    <partid>8901</partid> 
    <loc>MUM</loc> 
    <country>IND</country> 
    <cost>45000</cost> 
    <linenumber>1</linenumber> 
    <componentpartid>PID3</componentpartid> 
    <componenttype>PTYPE</componenttype> 
    <quantityperpart>28</quantityperpart> 
    </BomAssyV> 
</BomCollection> 

向該輸出XML

<request> 
    <sender>Oracle</sender> 
    <messageid>ABCD1234EFG</messageid> 
    <header> 
    <bomid>1234</bomid> 
    <bomid>4567</bomid> 
    <loc>DEL</loc> 
    <country>IND</country> 
    <cost>30</cost> 
    <line> 
     <lineNumber>1</lineNumber> 
     <componentPartId>CID1</componentPartId> 
     <componentType>CTYPE</componentType> 
     <quantityPerPart>2</quantityPerPart> 
    </line> 
    <line> 
     <lineNumber>2</lineNumber> 
     <componentPartId>CID2</componentPartId> 
     <componentType>CTYPE</componentType> 
     <quantityPerPart>30</quantityPerPart> 
    </line> 
    </header> 
    <header> 
    <bomid>4321</bomid> 
    <bomid>8901</bomid> 
    <loc>MUM</loc> 
    <country>IND</country> 
    <cost>45000</cost> 
    <line> 
     <lineNumber>1</lineNumber> 
     <componentPartId>PID3</componentPartId> 
     <componentType>PTYPE</componentType> 
     <quantityPerPart>28</quantityPerPart> 
    </line> 
    </header> 
</request> 
+0

非常感謝您的幫助。我將採取這一行動,繼續並驗證不同的有效載荷。 – user1948230