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

Re: Grouping the elements

Subject: Re: Grouping the elements
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Thu, 14 Sep 2006 18:21:48 +0530
2007 year of the element
Please try this stylesheet:

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

<xsl:output method="xml" indent="yes" />

<xsl:key name="x" match="Root/*" use="local-name()" />

   <xsl:template match="/Root">
        <Root>
            <xsl:for-each select="*[generate-id() =
generate-id(key('x', local-name())[1])]">
                <xsl:choose>
                    <xsl:when test="count(key('x', local-name())) &gt; 1">
                        <xsl:element name="{local-name()}List">
                            <xsl:copy-of select="key('x', local-name())" />
                        </xsl:element>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="key('x', local-name())" />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </Root>
   </xsl:template>

</xsl:stylesheet>

This when applied to XML:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
   <year>2006</year>
   <year>2007</year>
   <year>2008</year>
   <week>1</week>
   <day>01</day>
   <day>02</day>
</Root>

Produces the wanted result:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
  <yearList>
     <year>2006</year>
     <year>2007</year>
     <year>2008</year>
  </yearList>
  <week>1</week>
  <dayList>
     <day>01</day>
     <day>02</day>
  </dayList>
</Root>


On 9/14/06, Arulraj <p_arulraj@xxxxxxxxx> wrote:
Hi,

I have the following xml for my input..

<?xml version="1.0" encoding="UTF-8"?>
<Root>
   <year>2006</year>
   <year>2007</year>
   <year>2008</year>
   <week>1</week>
   <day>01</day>
   <day>02</day>
</Root>


My expected output is


<Root>
   <yearList>
       <year>2006</year>
        <year>2007</year>
       <year>2008</year>
    <yearList>
   <week>1</week>
   <dayList>
        <day>01</day>
        <day>02</day>
    </dayList>
</Root>


I want to group the element using xslt 1.0 and here element name is not fixed..it will be varied. when we have the same element then we need a parent element which have the element's name with List (i.e., yearList)


Is it possible to grouping in xslt1.0?

regards,
raj


--
Regards,
Mukul Gandhi

http://gandhimukul.tripod.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.