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 April 29th, 2008, 02:42 PM
Registered User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT conversion question

Here is the XML I have, anyone kw how i can use xslt to convert from

<city name="LA">
  <place>1</place>
</city>
<city name="LA">
  <place>2</place>
</city>
<city name="Boston">
  <place>1</place>
</city>

to

<city name="LA">
  <place>1</place>
  <place>2</place>
</city>
<city name="Boston">
  <place>1</place>
</city>


Thanks

Reply With Quote
  #2 (permalink)  
Old April 29th, 2008, 04:09 PM
Friend of Wrox
Points: 470, Level: 7
Points: 470, Level: 7 Points: 470, Level: 7 Points: 470, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: Plano, TX, USA.
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Hi,

I put <wrapper></wrapper> elements around your elements so I would have a single root element.

But here's the xslt:

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

    <xsl:template match="/">

        <wrapper>
            <xsl:text>#x0A;</xsl:text>
            <xsl:for-each-group select="//city" group-by="@name">
                <xsl:text>#x0A;</xsl:text>
                <xsl:element name="city">
                    <xsl:attribute name="name">
                        <xsl:value-of select="current-grouping-key()"/>
                    </xsl:attribute>

                    <xsl:for-each select="current-group()">
                        <xsl:text>#x0A;</xsl:text>
                        <xsl:element name="place">
                            <xsl:value-of select="./place"/>
                        </xsl:element>
                    </xsl:for-each>
                    <xsl:text>#x0A;</xsl:text>
                </xsl:element>
            </xsl:for-each-group>
            <xsl:text>#x0A;</xsl:text>
        </wrapper>
    </xsl:template>
</xsl:stylesheet>
It can be made more efficient, maybe not explicitly nameing the element and attribute names, but pulling them from the source nodes.

But I did this is just a few minutes.. so..

Hope this helps

- m

------------------------
GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
Reply With Quote
  #3 (permalink)  
Old April 29th, 2008, 09:00 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is a standard grouping problem, in fact it arrived just as I was about to start a session on grouping in the XSLT class I am teaching this week, so I used the example as a classroom exercise just to prove I can think on my feet.

Here's the XSLT 2.0 solution:

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:template match="/">
<cities>
  <xsl:for-each-group select="cities/city"
      group-by="@name">
      <city name="{current-grouping-key()}">
        <xsl:copy-of select="current-group()/place"/>
      </city>
  </xsl:for-each-group>
</cities>
</xsl:template>

</xsl:stylesheet>

For information on grouping in XSLT 1.0, see 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
  #4 (permalink)  
Old April 29th, 2008, 09:09 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

All this xsl:element and xsl:attribute stuff will work, but XSLT is already verbose enough without making it worse. Why not write this as

        <wrapper>
            <xsl:text>#x0A;</xsl:text>
            <xsl:for-each-group select="//city" group-by="@name">
                <xsl:text>#x0A;</xsl:text>
                <city name="{current-grouping-key()}">
                    <xsl:for-each select="current-group()">
                        <xsl:text>#x0A;</xsl:text>
                        <xsl:copy-of select="place"/>
                    </xsl:for-each>
                    <xsl:text>#x0A;</xsl:text>
                </city>
            </xsl:for-each-group>
            <xsl:text>#x0A;</xsl:text>
        </wrapper>

And you can probably simplify further by getting rid of the explicit newlines and relying on xsl:output indent="yes" instead.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
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
string to binary conversion in XSLT navi25584 XSLT 5 September 3rd, 2008 03:14 AM
XML to XSLT conversion?? Hannibal XSLT 16 November 24th, 2006 06:52 AM
Conversion Question dlandolin Access 2 July 30th, 2005 02:09 AM
XSLT unix timestamp conversion Weezel XSLT 2 January 28th, 2005 02:12 PM



All times are GMT -4. The time now is 02:09 PM.


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