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 May 20th, 2008, 04:22 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Subject: Convert XML using XSLT

Hello ,

I have a requirement to convert an xml document to another xml document using xslt. I am new to xml and xslt and need your help in solving this problem.

I have a xml file which looks like below
<Vendor>
<contact Id="1234" CmpnyNm="ABC" ContactNm="Sheryl">
   <Phone Number="555-5555" Type="WorkPhone"/>
</Contact>
<contact Id="1234" CmpnyNm="ABC" ContactNm="Sheryl">
   <Phone Number="666-6666" Type="WorkPhone"/>
</Contact>
<contact Id="1234" CmpnyNm="ABC" ContactNm="Sheryl">
   <Phone Number="999-9999" Type="WorkPhone"/>
</Contact>
</Vendor>

Since the attributes in the contact element, Id,CmpnyNm and ContactNm are same for the three recs and only the phone number is different, I want to have the 3 phone numbers under one contact element itself.

Please see below how I need to have the output xml.

<vendor>
<contact Id="1234" cmpnyNm="ABC" ContactNm="Sheryl">
   <Phone Number="555-5555" Type="WorkPhone"/>
   <Phone Number="666-6666" Type="WorkPhone"/>
   <Phone Number="999-9999" Type="WorkPhone"/>
</Contact>
</Vendor>

Is it possible to have the output in this format?. I tried xslt but looks like it needs complex logic in the xslt to achieve the output, but unfortunately I am not good at xml and xslt.

Any help in solving this problem would be greatly appreciated.

Thank you
Manoj
Reply With Quote
  #2 (permalink)  
Old May 20th, 2008, 04:37 PM
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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is a standard grouping problem. In XSLT 2.0 you can solve such problems using <xsl:for-each-group>, for example

<xsl:template match="Vendor">
  <vendor>
     <xsl:for-each-group select="contact" group-by="@Id">
       <contact>
         <xsl:copy-of select="@*, current-group()/Phone"/>
       </contact>
     </xsl:for-each-group>
  </vendor>
</xsl:template>

Grouping problems are much more difficult in XSLT 1.0, but there is a standard coding pattern called Muenchian grouping for solving them; you can find details in any good XSLT book or at http://www.jenitennison.com/xslt/grouping

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old May 21st, 2008, 12:56 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Mike,

Thank you for your quick response. I will try to create the xslt based on your recommendation and give it a try.

Thanks again
Manoj

Reply With Quote
  #4 (permalink)  
Old May 29th, 2008, 02:07 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Folks,
First of all My thanks to Mike for the tip with jennitennison portal link. I was able to get the grouping done for my input xml. I am getting stuck with the tags and I need help with your expertise

For my input which I described above(starting of this post) I created the following XSLT file

<?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" version="1.0"/>


<xsl:key name="contacts-by-id" match="Contact" use="@Id" />
<xsl:template match="Vendor">

Vendor <br/>

    <xsl:for-each select="Contact[count(. | key('contacts-by-id', @Id)[1]) = 1]">
    Contact Id="<xsl:value-of select="@Id" />" CmpnyNm="<xsl:value-of select="@CmpnyNm"/>" ContactNm="<xsl:value-of

select="@ContactNm"/>" <br/>
        <xsl:for-each select="key('contacts-by-id', @Id)">
            Phone Number="<xsl:value-of select="Phone/@Number" />" Type="<xsl:value-of select="Phone/@Type"/>" <br/>
        </xsl:for-each>
    </xsl:for-each>
Vendor
</xsl:template>

</xsl:stylesheet>

For the Above XSLT the output looks like

Vendor
contact Id="1234" cmpnyNm="ABC" ContactNm="Sheryl"
   Phone Number="555-5555" Type="WorkPhone"
   Phone Number="666-6666" Type="WorkPhone"
   Phone Number="999-9999" Type="WorkPhone"
Contact

Vendor

As you can see, for some reason it is not populating the tags even when I mentioned output method="xml". I want the open and close tags for the output to look like expected output xml I described above(starting of this post). Is it possible to get that?.
I tried many ways(In Some cases I got xslt error and in some cases there was no output) and after googling a lot nothing worked out. So
wanted to try with you guys.

Note: I was able to get it this way, but I want the attributes to be enclosed in the < /> tags.

<contact>Id="1234" cmpnyNm="ABC" ContactNm="Sheryl"
<Phone>Number="555-5555" Type="WorkPhone"</Phone>
<Phone>Number="666-6666" Type="WorkPhone"</Phone>
.
.
</Contact>

Thanks for your help.
Manoj

Reply With Quote
  #5 (permalink)  
Old May 29th, 2008, 02:33 PM
Authorized User
 
Join Date: May 2008
Location: Moscow, , Russia.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Note: I was able to get it this way, but I want the attributes to be enclosed in the < /> tags.
look for <xsl:attribute> element.

Reply With Quote
  #6 (permalink)  
Old May 29th, 2008, 02:39 PM
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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Astounding! - you've got Muenchian grouping to work, but you don't know how to output elements and attributes. Talk of running before you've learnt to walk...

This line

    Contact Id="<xsl:value-of select="@Id" />" CmpnyNm="<xsl:value-of select="@CmpnyNm"/>" ContactNm="<xsl:value-of

select="@ContactNm"/>"

should look like this:

<Contact Id="{@Id}" CmpnyNm="{@CmpnyNm}" ContactNm="{@ContactNm}"/>

and similar changes elsewhere.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #7 (permalink)  
Old May 30th, 2008, 04:27 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Mike,

Mostly Concentrated on working on the grouping and did not knew much about the xsl:attribute element.

I got it working with the <xsl:attribute> element.

Thanks a lot Volder and Mike for your inputs and help.

Manoj

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
Can XSLT convert amount to words LeoMathew XSLT 4 January 10th, 2018 04:09 AM
Convert 1 big XML to multiple smaller XML victorcorey XSLT 1 March 20th, 2008 05:59 AM
Convert XML to XML template question bonekrusher XSLT 3 July 12th, 2007 07:47 AM
convert XML to XMl - Another Namespace question bonekrusher XSLT 2 July 10th, 2007 07:32 AM
Using XSLT to convert XML to a table ? nobitavn94 XSLT 3 October 30th, 2006 11:03 AM



All times are GMT -4. The time now is 11:22 PM.


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