Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 6th, 2003, 03:30 PM
POL POL is offline
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ordering lists

I have a list of several dozens cities in XML format organized in no particular order such as this:

<cities>
<city>
<cityName>San Francisco</cityName>
<stateName>California</stateName>
</city>

<city>
<cityName>Charlotte</cityName>
<stateName>North Carolina</stateName>
</city>

...
</cities>


By using XSLT I want to organize in alphabetic order by states and by cities within each state, the state name should appear only once on top of cities located within every particular state.

State name a
  City name A
  City name B
  City name C
  ...

State name b
  City name A
  City name B
  City name C
  City name D
  ...
...

I tried this:

<xsl:apply-templates select="cities/city">
<xsl:sort select="stateName" />
<xsl:sort select="cityName" />
</xsl:apply-templates>

<xsl:template match="city">

<xsl:if test="preceding-sibling::*[1]/stateName != stateName" >
<xsl:value-of select="stateName" />
</xsl:if>
<xsl:value-of select="cityName" />
</xsl:template>

The problem is that once I execute this all cities are preceded by a stateName.

Thanks for helping me.
Juan
  #2 (permalink)  
Old June 9th, 2003, 02:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

I put all the stuff in the "root template", though it will be better to break it down to several templates:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:variable name="set-of-states" select="/cities/city/stateName[not(. = preceding::stateName)]"/>
        <xsl:for-each select="$set-of-states">
            <xsl:sort select="."/>
            <br/>
            [<xsl:value-of select="."/>]<br/>
            <xsl:for-each select="/cities/city/cityName[../stateName = current()]">
                <xsl:sort select="."/>
                <xsl:value-of select="."/><br/>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
  #3 (permalink)  
Old June 9th, 2003, 11:25 AM
POL POL is offline
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Armmarti
many, many thanks for your answer, it worked and I'm really happy, thanks to you.

I wish you a really good week!

Thanks
Juan


Quote:
quote:Originally posted by armmarti
 I put all the stuff in the "root template", though it will be better to break it down to several templates:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:variable name="set-of-states" select="/cities/city/stateName[not(. = preceding::stateName)]"/>
        <xsl:for-each select="$set-of-states">
            <xsl:sort select="."/>
            <br/>
            [<xsl:value-of select="."/>]<br/>
            <xsl:for-each select="/cities/city/cityName[../stateName = current()]">
                <xsl:sort select="."/>
                <xsl:value-of select="."/><br/>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
  #4 (permalink)  
Old June 9th, 2003, 11:39 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

You are welcome POL


Similar Threads
Thread Thread Starter Forum Replies Last Post
stupid question to ordering column in table sal21 BOOK: Access 2003 VBA Programmer's Reference 0 May 9th, 2007 08:00 AM
Ordering rows problem ami4quest6 SQL Language 10 October 12th, 2005 01:13 AM
Ordering a dataview Louisa VB.NET 2002/2003 Basics 1 November 11th, 2004 10:04 AM
Ordering results EstherMStrom XSLT 6 October 13th, 2004 09:50 AM
alternate ordering polecat XSLT 17 October 2nd, 2003 08:35 AM





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