0

I have a source XML which is like this (as shown below) in which there is one PR tag under which there can be multiple Pr Line tags but in the final XML i want the source XML (containing multiple Pr Line tags) to be split as shown in the target xml.

Source:

  <PRSet>
   <PR>
    <PRNUM>100</PRNUM> 
    <VENDOR>XYZ LLC</VENDOR> 
    <PRLINE>
      <PRLINENUM>101</PRLINENUM>
      <DESCRIPTION>Burner Lamp</DESCRIPTION> 
    </PRLINE>

    <PRLINE>
      <PRLINENUM>102</PRLINENUM>
      <DESCRIPTION>Stove</DESCRIPTION>
    </PRLINE>
   </PR>
  </PRSet>

When multiple PRLine Tags are there in a single PR tag then the final xml should look like this:

Target:

<PRSet>
 <PR>
  <PRNUM>100</PRNUM> 
  <VENDOR>XYZ LLC</VENDOR> 
 <PRLINE>
  <PRLINENUM>101</PRLINENUM>
  <DESCRIPTION>Burner Lamp</DESCRIPTION> 
 </PRLINE>
</PR>

 <PR>
  <PRNUM>100</PRNUM> 
  <VENDOR>XYZ LLC</VENDOR> 
 <PRLINE>
  <PRLINENUM>102</PRLINENUM>
  <DESCRIPTION>Stove</DESCRIPTION>
 </PRLINE>
</PR>

Can anybody provide the solution how to achieve this using XSLT Transformation. Many Thanks in Advance.

1 Answer 1

1

The transformation that you describe can be achieved with the following XSLT stylesheet:

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

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

    <xsl:template match="PRSet">
        <xsl:copy>
            <xsl:apply-templates select="PR/PRLINE" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="PRLINE">
        <PR>
            <xsl:copy-of select="../PRNUM|../VENDOR|." />
        </PR>
    </xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.