xsl-list
[Top] [All Lists]

Re: XSLT to conver flat XML to Heirarchy XML

2005-04-26 23:19:17
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(_dot_)gangula(_at_)wipro(_dot_)com 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(_at_)wipro(_dot_)com
immediately
and destroy all copies of this message and any
attachments.


--~------------------------------------------------------------------
XSL-List info and archive: 
http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to:
http://lists.mulberrytech.com/xsl-list/
or e-mail:
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>