xsl-list
[Top] [All Lists]

Re: [xsl] Grouping the elements

2006-09-14 05:34:31
Hi Raj,

Here's a not-so-orthodox solution (I think, not sure though) to your inquiry. It works as follows: grab the last item of each element and create a "List" element or a "copy" element to the output stream depending on the count of the node. If it is supposed to become a "List" element, it will copy all the nodes with the same name into that list. Here's the code (hope the lines are not messed up by the mailer):

<?xml version="1.0" encoding="UTF-8"?>
<?altova_samplexml testRaj.xml?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; >
<xsl:output indent="yes" method="xml" />

<xsl:template match="Root">
   <xsl:copy>
       <xsl:apply-templates
           select="year[position() = last()]" />
       <xsl:apply-templates
           select="month[position() = last()]" />
       <xsl:apply-templates
           select="week[position() = last()]" />
       <xsl:apply-templates
           select="day[position() = last()]" />
   </xsl:copy>
</xsl:template>

<xsl:template match="*">
   <xsl:variable name="curName" select="name()" />
   <xsl:if test="count(../*[name() = $curName]) > 1">
       <xsl:element name="{local-name(.)}List">
           <xsl:copy-of select="../*[name() = $curName]" />
       </xsl:element>
   </xsl:if>
   <xsl:if test="count(../*[name() = $curName]) = 1">
       <xsl:copy-of select="." />
   </xsl:if>
</xsl:template>

</xsl:stylesheet>


Best regards,
Abel Braaksma
http://abelleba.metacarpus.com


Arulraj 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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--