0

I would need the help in XSLT 1.0 to transform the following Source XML to Result XML. Not very acquainted with XSLT and also searching for the useful posts in this forum simultaneously. thanks for your help.

Source 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>

Result 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
    This question is a bit vague. If you're looking for a tutorial in XSLT then google is probably your friend. If you've got something a little more concrete we'd be happy to help. – Will Jan 04 '13 at 12:11
  • Going through w3c tutorials. But need the help on the requirement mentioned above. Need to convert Source XML to Result XML which are given in my post. – user1948230 Jan 04 '13 at 12:42
  • 3
    This is a pretty straight-forward transformation. Please provide your current XSLT template and specific questions instead of just asking people to code the entire thing for you. – Jan Wikholm Jan 04 '13 at 12:46
  • @user1948230: I am not aware of any W3C XSLT tutorial (please note that W3schools is not related to W3C). You should learn XSLT with [Norman Walsh's tutorial](http://nwalsh.com/docs/tutorials/xsl/). – Aurélien Bénel Jan 04 '13 at 13:18
  • We don't wanna do your homework! – Rookie Programmer Aravind Jan 04 '13 at 13:45
  • You need to say where you are stuck, why you are finding it difficult. Otherwise this simply looks like a request for free coding and/or free training. – Michael Kay Jan 04 '13 at 15:32

1 Answers1

0

You need to do some grouping - have a look at this SO Post : Applying Muenchian grouping for a simple XML with XSLT. Specifically, look at the reference to Jeni Tennison's Muenchain grouping guide

I've assumed here that there is only one request output element (otherwise you'll need a second level of grouping), and also that bomid and partid are tied.

The 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>

Transforms this input 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>

To this output 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>
Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285