[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: XSLT to conver flat XML to Heirarchy XML

Subject: RE: XSLT to conver flat XML to Heirarchy XML
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Wed, 27 Apr 2005 02:51:44 -0700 (PDT)
conver xml to xml xslt
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:element name="{//Record[1]/keyfieldValue}">
    <xsl:copy-of
select="//Record[1]/*[not(self::keyfieldValue)]" />
    <xsl:element name="{//Record[2]/keyfieldValue}">
      <xsl:copy-of
select="//Record[2]/*[not(self::keyfieldValue)]" />   
  
      <xsl:call-template name="xyz">
        <xsl:with-param name="x" select="//Record[3]"
/>
      </xsl:call-template>
      <APPOINTMENT>
        <xsl:copy-of select="//Record[keyfieldValue =
'APPOINTMENT']/*" />
      </APPOINTMENT>
      <xsl:for-each select="//Record[keyfieldValue =
'NAME']">
        <NAME>
          <xsl:copy-of select="*" />
          <ADDRESS>
            <xsl:copy-of
select="following-sibling::Record[1]/*" />
          </ADDRESS>
        </NAME>
      </xsl:for-each>
    </xsl:element> 
  </xsl:element>
</xsl:template>

<xsl:template name="xyz">
  <xsl:param name="x" />
  
  <xsl:if test="$x and not($x/keyfieldValue =
'APPOINTMENT') and not($x/keyfieldValue = 'NAME') and
not(($x/keyfieldValue = 'ADDRS') and
($x/preceding-sibling::Record[1]/keyfieldValue =
'NAME'))">
    <xsl:element name="{$x/keyfieldValue}">
      <xsl:copy-of select="$x/fieldValue" />
      <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 have slightly changed the syntax (as compared to my
previous stylesheet).. I am now using <xsl:copy-of
instead of xsl:for-each (for the syntactic
convenience).. Also to simply things, I have removed
the namespace declerations from source XML..

When the above stylesheet is applied to XML -
<ns>
  <RECSETNAME>
    <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>

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

Regards,
Mukul

--- sreekanth.gangula@xxxxxxxxx wrote:

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


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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.