Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 31st, 2009, 07:26 AM
Authorized User
 
Join Date: Dec 2009
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Thumbs up Reconstruct XML using XSLT in Tree View

Hi there,

I have following XML and i want to transform it to my desire XML using XSLT ,i have succeded somewhat but not whole problem is solved .So
I need help

Given is My Input XML

<?xml version = '1.0'?>
<ROWSET>
<irp_account num="1">
<IRP_CARRIER_ID_NUMBER>274845</IRP_CARRIER_ID_NUMBER>
<IRP_ACCOUNT_NUMBER>55002</IRP_ACCOUNT_NUMBER>
</irp_account>
<irp_account num="83">
<IRP_CARRIER_ID_NUMBER>928265</IRP_CARRIER_ID_NUMBER>
<IRP_ACCOUNT_NUMBER>59897</IRP_ACCOUNT_NUMBER>
<NAME_TYPE>LG</NAME_TYPE>
<NAME>LAMBERT TRUCKING INC</NAME>
<ADDRESS_TYPE>PH</ADDRESS_TYPE>
</irp_account>
<irp_account num="97">
<IRP_CARRIER_ID_NUMBER>957858</IRP_CARRIER_ID_NUMBER>
<IRP_ACCOUNT_NUMBER>59940</IRP_ACCOUNT_NUMBER>
<NAME_TYPE>LG</NAME_TYPE>
<NAME>SONNY DAVIS INC</NAME>
<ADDRESS_TYPE>MA</ADDRESS_TYPE>
</irp_account>
<irp_account num="98">
<IRP_CARRIER_ID_NUMBER>957858</IRP_CARRIER_ID_NUMBER>
<IRP_ACCOUNT_NUMBER>59940</IRP_ACCOUNT_NUMBER>
<NAME_TYPE>LG</NAME_TYPE>
<NAME>SONNY DAVIS INC</NAME>
<ADDRESS_TYPE>PH</ADDRESS_TYPE>
</irp_account>
</ROWSET>


With the use of XSLT i want to generate Output XML like this.

<?xml version="1.0"?>
<T0020>
<IRP_ACCOUNT>
<IRP_CARRIER_ID_NUMBER>274845</IRP_CARRIER_ID_NUMBER>
<IRP_ACCOUNT_NUMBER>55002</IRP_ACCOUNT_NUMBER>
<IRP_NAME>
<NAME_TYPE>LG</NAME_TYPE>
<NAME>A P SUPPLY CO</NAME>
<IRP_ADDRESS>
<ADDRESS_TYPE>PH</ADDRESS_TYPE>
</IRP_ADDRESS>
<IRP_ADDRESS>
<ADDRESS_TYPE>MA</ADDRESS_TYPE>
</IRP_ADDRESS>
</IRP_NAME>
</IRP_ACCOUNT>
</T0020>


I have tried below XSLT but it is not giving desire result


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/ROWSET">
<xsl:element name="T0020">
<xsl:apply-templates select="irp_account"/>
</xsl:element>
</xsl:template>
<xsl:template match="irp_account">
<xsl:element name="IRP_ACCOUNT">
<xsl:apply-templates select="IRP_CARRIER_ID_NUMBER"/>
<xsl:apply-templates select="IRP_ACCOUNT_NUMBER"/>
<xsl:apply-templates select="IRP_ACCOUNT_TYPE"/>
<xsl:apply-templates select="NAME_TYPE"/>
<xsl:apply-templates select="ADDRESS_TYPE"/>
</xsl:element>
</xsl:template>
<xsl:template match="IRP_CARRIER_ID_NUMBER">
<xsl:copy-of select="descendant-or-self::IRP_CARRIER_ID_NUMBER"/>
</xsl:template>
<xsl:template match="IRP_ACCOUNT_NUMBER">
<xsl:copy-of select="descendant-or-self::IRP_ACCOUNT_NUMBER"/>
</xsl:template>
<xsl:template match="IRP_ACCOUNT_TYPE">
<xsl:copy-of select="descendant-or-self::IRP_ACCOUNT_TYPE"/>
</xsl:template>
<xsl:template match="NAME_TYPE">
<xsl:element name="IRP_NAME">
<xsl:copy-of select="descendant-or-self::NAME_TYPE"/>
<xsl:copy-of select="following-sibling::NAME"/>
</xsl:element>
</xsl:template>
<xsl:template match="ADDRESS_TYPE">
<xsl:element name="IRP_ADDRESS">
<xsl:copy-of select="descendant-or-self::ADDRESS_TYPE"/>
<xsl:copy-of select="following-sibling::NAME"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Please help ....
Reply With Quote
  #2 (permalink)  
Old December 31st, 2009, 07:36 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

The input sample you posted has four 'irp_account' elements, the result sample you posted has only one 'IRP_ACCOUNT' element. Do you want to only process the first 'irp_account' element? Or is the result sample not complete?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #3 (permalink)  
Old December 31st, 2009, 08:02 AM
Authorized User
 
Join Date: Dec 2009
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Default

No this one is only sample in input XML i just want to show that there my be a
chance that Address type for a particular IRP_ACCOUNT may be PH(Physical Address) or both (Physical and Mailing).

I have shown desired Output XML as only one element to save space.

Thanks Martin Honnen for your reply..
Reply With Quote
  #4 (permalink)  
Old December 31st, 2009, 08:21 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try something like this:

Code:
  <xsl:template match="irp_account">
    <IRP_ACCOUNT>
        <xsl:apply-templates select="IRP_CARRIER_ID_NUMBER"/>
        <xsl:apply-templates select="IRP_ACCOUNT_NUMBER"/>
        <IRP_NAME>
          <xsl:apply-templates select="IRP_ACCOUNT_TYPE"/>
          <xsl:apply-templates select="NAME_TYPE"/>    
          <xsl:apply-templates select="ADDRESS_TYPE"/>
        </IRP_NAME>
    </IRP_ACCOUNT>    
  </xsl:template>

  <xsl:template match="NAME_TYPE">    
     <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="NAME">    
     <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="ADDRESS_TYPE">
    <IRP_ADDRESS>    
        <xsl:copy-of select="."/>
    </IRP_ADDRESS>
   </xsl:template>
The templates that you have written like this:

Code:
   <xsl:template match="IRP_CARRIER_ID_NUMBER">
     <xsl:copy-of select="descendant-or-self::IRP_CARRIER_ID_NUMBER"/>
   </xsl:template>
aren't wrong, but they had me very confused: it would be much clearer to write <xsl:copy-of select="."/>. Alternatively, you could use the "identity template" pattern where the default template for all elements is

[code]
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
[/copy]

and then only override this when you want to do something other than copy the element unchanged.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #5 (permalink)  
Old December 31st, 2009, 08:23 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well usually if you want to transform XML to XML and you present an input sample and an output sample then the output sample should be exactly what you want to produce from the input sample presented.
Otherwise it is not clear what you want to achieve.
If you want to map 'irp_account' in the input XML to 'IRP_ACCOUNT' elements in the output XML then that is easy to achieve and with less verbosity than you have:
[code
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="ROWSET">
<T0020>
<xsl:apply-templates/>
</T0020>
</xsl:template>

<xsl:template match="irp_account">
<IRP_ACCOUNT>
<xsl:copy-of select="IRP_CARRIER_ID_NUMBER | IRP_ACCOUNT_NUMBER"/>
</IRP_ACCOUNT>
</xsl:template>

</xsl:stylesheet>
[/code]
I am afraid I can't tell where, for instance for the first 'irp_account' element, you would get the output elements like 'NAME' or 'NAME_TYPE' from.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tree View dotnetDeveloper ASP.NET 3.5 Basics 1 November 26th, 2008 05:05 PM
Flat XML structure to a tree by XSLT Borg0011 XSLT 2 July 9th, 2006 08:58 AM
How to use the xml and XSLT to create a Tree struc umeshayk XSLT 1 December 29th, 2003 08:09 AM
Tree View and XML bmains Pro VB.NET 2002/2003 0 October 30th, 2003 12:46 PM
Tree View Natalie Pro VB 6 5 September 23rd, 2003 07:23 AM



All times are GMT -4. The time now is 09:22 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.