RE: [xsl] XSLT to conver flat XML to Heirarchy XML

Subject: RE: [xsl] XSLT to conver flat XML to Heirarchy XML
From: <sreekanth.gangula@xxxxxxxxx>
Date: Wed, 27 Apr 2005 12:56:34 +0530
Hi Mukul ,
  Thanks a lot. But I have got two more elements at the end.
  This requirement I got it a couple of hours ago.
  I am just wondering whether is it possible.
  Your help is really appreciated. Thanks in adavnce.
  <ns:MT_Test xmlns:ns="http://Centrica/Test";>
  <RECSETNAME xmlns:ns="http://Centrica/Test";>
    <Record>
        <keyfieldValue>HEADR</keyfieldValue>
        <fieldValue>CDJOB</fieldValue>
        <fieldValue>TRA</fieldValue>
    </Record>
    <Record>
        <keyfieldValue>TRANS</keyfieldValue>
        <fieldValue>DATA</fieldValue>
        <fieldValue>EXCHG</fieldValue>
        <fieldValue>EXCH</fieldValue>
    </Record>
    <Record>
        <keyfieldValue>MTPNT</keyfieldValue>
        <fieldValue>74842606</fieldValue>
    </Record>
    <Record>
        <keyfieldValue>ADDRS</keyfieldValue>
        <fieldValue>MTRPT</fieldValue>
        <fieldValue>BRITISH TELECOM</fieldValue>
    </Record>
    <Record>
        <keyfieldValue>ASSET</keyfieldValue>
        <fieldValue>INSTL</fieldValue>
        <fieldValue>METER</fieldValue>
    </Record>
    <Record>
        <keyfieldValue>METER</keyfieldValue>
        <fieldValue>T</fieldValue>
    </Record>
   <Record>
        <keyfieldValue>APPOINTMENT</keyfieldValue>
        <fieldValue>T</fieldValue>
    </Record>
    <Record>
        <keyfieldValue>NAME</keyfieldValue>
        <fieldValue>TEST</fieldValue>
        <fieldValue>BRITISH TELECOM</fieldValue>
    </Record>
    <Record>
        <keyfieldValue>ADDRS</keyfieldValue>
        <fieldValue>NAME</fieldValue>
        <fieldValue>BRITISH TELECOM</fieldValue>
    </Record>
  </RECSETNAME>
</ns:MT_Test>

The result of this looks like:
<?xml version="1.0" encoding="UTF-8"?>
<HEADR>
   <fieldValue>CDJOB</fieldValue>
   <fieldValue>TRA</fieldValue>
   <TRANS>
      <fieldValue>DATA</fieldValue>
      <fieldValue>EXCHG</fieldValue>
      <fieldValue>EXCH</fieldValue>
      <MTPNT>
         <fieldValue>74842606</fieldValue>
         <ADDRS>
            <fieldValue>MTRPT</fieldValue>
            <fieldValue>BRITISH TELECOM</fieldValue>
            <ASSET>
               <fieldValue>INSTL</fieldValue>
               <fieldValue>METER</fieldValue>
               <METER>
                  <fieldValue>T</fieldValue>
               </METER>
            </ASSET>
         </ADDRS>
      </MTPNT>
      <APPOINTMENT>
         <keyfieldValue>APPOINTMENT</keyfieldValue>
        <fieldValue>T</fieldValue>
     </APPOINTMENT>
     <NAME>
        <keyfieldValue>NAME</keyfieldValue>
        <fieldValue>TEST</fieldValue>
        <fieldValue>BRITISH TELECOM</fieldValue>
       <ADDRESS>
        <keyfieldValue>ADDRS</keyfieldValue>
        <fieldValue>NAME</fieldValue>
        <fieldValue>BRITISH TELECOM</fieldValue>
       </ADDRESS>
     </NAME>
   </TRANS>
</HEADR>


-----Original Message-----
From: Mukul Gandhi [mailto:mukul_gandhi@xxxxxxxxx]
Sent: Wednesday, April 27, 2005 11:49 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] XSLT to conver flat XML to Heirarchy XML

Please try this XSL..

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

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

<xsl:template match="/">
  <xsl:call-template name="xyz">
    <xsl:with-param name="x" select="//Record[1]" />
  </xsl:call-template>
</xsl:template>

<xsl:template name="xyz">
  <xsl:param name="x" />

  <xsl:if test="$x">
    <xsl:element name="{$x/keyfieldValue}">
      <xsl:for-each select="$x/fieldValue">
        <fieldValue><xsl:value-of select="."
/></fieldValue>
      </xsl:for-each>
      <xsl:call-template name="xyz">
        <xsl:with-param name="x"
select="$x/following-sibling::Record[1]" />
      </xsl:call-template>
    </xsl:element>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

I feel double quote characters (") in element values are erroneous (or
you require them?).. So I omitted "
from the XML document.

When the above XSLT stylesheet is applied to XML -

<ns:MT_Test xmlns:ns="http://Centrica/Test";>
  <RECSETNAME xmlns:ns="http://Centrica/Test";>
    <Record>
        <keyfieldValue>HEADR</keyfieldValue>
        <fieldValue>CDJOB</fieldValue>
        <fieldValue>TRA</fieldValue>
    </Record>
    <Record>
        <keyfieldValue>TRANS</keyfieldValue>
        <fieldValue>DATA</fieldValue>
        <fieldValue>EXCHG</fieldValue>
        <fieldValue>EXCH</fieldValue>
    </Record>
    <Record>
        <keyfieldValue>MTPNT</keyfieldValue>
        <fieldValue>74842606</fieldValue>
    </Record>
    <Record>
        <keyfieldValue>ADDRS</keyfieldValue>
        <fieldValue>MTRPT</fieldValue>
        <fieldValue>BRITISH TELECOM</fieldValue>
    </Record>
    <Record>
        <keyfieldValue>ASSET</keyfieldValue>
        <fieldValue>INSTL</fieldValue>
        <fieldValue>METER</fieldValue>
    </Record>
    <Record>
        <keyfieldValue>METER</keyfieldValue>
        <fieldValue>T</fieldValue>
    </Record>
  </RECSETNAME>
</ns:MT_Test>

The output recieved is -

<?xml version="1.0" encoding="UTF-8"?>
<HEADR>
   <fieldValue>CDJOB</fieldValue>
   <fieldValue>TRA</fieldValue>
   <TRANS>
      <fieldValue>DATA</fieldValue>
      <fieldValue>EXCHG</fieldValue>
      <fieldValue>EXCH</fieldValue>
      <MTPNT>
         <fieldValue>74842606</fieldValue>
         <ADDRS>
            <fieldValue>MTRPT</fieldValue>
            <fieldValue>BRITISH TELECOM</fieldValue>
            <ASSET>
               <fieldValue>INSTL</fieldValue>
               <fieldValue>METER</fieldValue>
               <METER>
                  <fieldValue>T</fieldValue>
               </METER>
            </ASSET>
         </ADDRS>
      </MTPNT>
   </TRANS>
</HEADR>

Regards,
Mukul

--- sreekanth.gangula@xxxxxxxxx wrote:
>
> Hi ,
> >  I have an XML which is flat,
> >
> ><?xml version="1.0" encoding="utf-8" ?>
> >
> ><ns:MT_Test
>
xmlns:ns="<http://centrica/Test>http://Centrica/Test";>
> >  <RECSETNAME
>
xmlns:ns="<http://centrica/Test>http://Centrica/Test";>
> >    <Record>
> >        <keyfieldValue>"HEADR"</keyfieldValue>
> >        <fieldValue>"CDJOB"</fieldValue>
> >        <fieldValue>"TRA"</fieldValue>
> >    </Record>
> >
> >    <Record>
> >        <keyfieldValue>"TRANS"</keyfieldValue>
> >        <fieldValue>"DATA"</fieldValue>
> >        <fieldValue>"EXCHG"</fieldValue>
> >        <fieldValue>"EXCH"</fieldValue>
> >    </Record>
> >
> >    <Record>
> >        <keyfieldValue>"MTPNT"</keyfieldValue>
> >        <fieldValue>74842606</fieldValue>
> >    </Record>
> >
> >    <Record>
> >        <keyfieldValue>"ADDRS"</keyfieldValue>
> >        <fieldValue>"MTRPT"</fieldValue>
> >        <fieldValue>"BRITISH TELECOM"</fieldValue>
> >    </Record>
> >
> >    <Record>
> >        <keyfieldValue>"ASSET"</keyfieldValue>
> >        <fieldValue>"INSTL"</fieldValue>
> >        <fieldValue>"METER"</fieldValue>
> >    </Record>
> >
> >    <Record>
> >        <keyfieldValue>"METER"</keyfieldValue>
> >        <fieldValue>"T"</fieldValue>
> >    </Record>
> >
> >  </RECSETNAME>
> ></<ns:MT_Test>
> >
> >All the data is store under the record structure,
> based on the
> >keyFieldValue i have to generate the target node.
> >
> >my Target XML should look like:
> >
> ><HEADR>
> >   <fieldValue>"CDJOB"</fieldValue>
> >   <fieldValue>"TRA"</fieldValue>
> >   <TRANS>
> >     <fieldValue>"DATA"</fieldValue>
> >     <fieldValue>"EXCHG"</fieldValue>
> >     <fieldValue>"EXCH"</fieldValue>
> >
> >     <MTPNT>
> >       <fieldValue>74842606</fieldValue>
> >
> >       <ADDRS>
> >          <fieldValue>"MTRPT"</fieldValue>
> >          <fieldValue>"BRITISH
> TELECOM"</fieldValue>
> >       </ADDRS>
> >
> >       <ASSET>
> >          <fieldValue>"INSTL"</fieldValue>
> >          <fieldValue>"568"</fieldValue>
> >
> >          <METER>
> >             <fieldValue>"T"</fieldValue>
> >          </METER>
> >       </ASSET>
> >     <MTPNT>
> >   </TRANS>
> ></HEADR>
> >
> >
> >
> >The logic for generating:
> >
> >   loop through the records in sorce xml,
> >
> >    Base on the key value generate the note in the
> tareget XML, The
> >problem here is i am not sure how to insert the
> nodes as a children in
> >the targt.
> >
> >
> >
> >Could anyone please help.
> >
> >
> >
> >Regards
> >
> >Sreekanth
>
>
>
> Confidentiality Notice
>
> The information contained in this electronic message and any
> attachments to this message are intended for the exclusive use of the
> addressee(s) and may contain confidential or privileged information.
> If you are not the intended recipient, please notify the sender at
> Wipro or Mailadmin@xxxxxxxxx immediately and destroy all copies of
> this message and any attachments.
>
>

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com




Confidentiality Notice

The information contained in this electronic message and any attachments to
this message are intended
for the exclusive use of the addressee(s) and may contain confidential or
privileged information. If
you are not the intended recipient, please notify the sender at Wipro or
Mailadmin@xxxxxxxxx immediately
and destroy all copies of this message and any attachments.

Current Thread