Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old October 26th, 2007, 06:18 PM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default grouping logic

Hi All,

My input is like this

 <ItemList>
    <Item Code="01234" Name="COLA">
      <List>
        <Number="104004" Code="u"/>
      </List>
      <CList>
        <st Level1="4535108" Date="2007-11-02" />
      </CList>
    </Item>
   <Item Code="01234" Name="COLA">
      <List>
        <Number="104004" Code="u"/>
      </List>
      <CList>
<st Level1="4535222" Date="2007-11-02" />
      </CList>
    </Item>
 </ItemList>

Whenever the code appears the same (in this case it is 01234) group all the elements List and cList elements
The output should look like this:


 <Item Code="01234" Name="COLA">
      <List>
        <Number="104004" Code="u"/>
      </List>
      <CList>
        <st Level1="4535108" Date="2007-11-02" />
       <st Level1="4535222" Date="2007-11-02" />
     </CList>
    </Item>
 </ItemList>

Please give me some suggestion and ideas.





 
Old October 26th, 2007, 07:01 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You should get plenty of ideas by googling for "XSLT grouping" or looking up "grouping" in the index of your favourite XSLT book.

In XSLT 2.0 use the xsl:for-each-group instruction.

In 1.0 use Muenchian grouping described 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
 
Old October 26th, 2007, 10:08 PM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried different combinations from the website you have mentioned, but I didn't had success, so if someone can give some tips that would be really great.

Thanks

 
Old October 27th, 2007, 03:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well, I can't write you a better tutorial than Jeni has done. But if you show your code, then I can see where you went wrong, and I can help you to fix it.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 27th, 2007, 05:21 AM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Thanks for the reply, Here is the code I am using,

<?xml version="1.0"?>
<xsl:stylesheet exclude-result-prefixes="msxsl" version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:key name="rows" match="Item" use="Code"/>

<xsl:template match="/">
<Document>
<xsl:call-template name="book"/>
</Document>
</xsl:template>

<xsl:template name="book">
<xsl:for-each select="//Document">
<xsl:attribute name="Name">
<xsl:value-of select="./@Name"/>
</xsl:attribute>
<xsl:for-each select="./ItemList">
<ItemList>
<xsl:for-each select="//Item[generate-id(.) = generate-id(key('rows',Code)[1])]" >
<Item>

<xsl:attribute name="Code">
<xsl:value-of select="./@Code"/>
</xsl:attribute>
<xsl:attribute name="Name">
<xsl:value-of select="./@Name"/>
</xsl:attribute>
</Item>

<List>
<xsl:for-each select="./List/Number">
<BList>
<xsl:attribute name="Number">
<xsl:value-of select="./@Number"/>
</xsl:attribute>
<xsl:attribute name="Code">
<xsl:value-of select="./@Code"/>
</xsl:attribute>
</BList>
</xsl:for-each>
</List>
<CList> <xsl:for-each select="./CList/st">
<st>
<xsl:attribute name="Level1">
<xsl:value-of select="./@Level1"/>
</xsl:attribute>
<xsl:attribute name="Date"><xsl:value-of select="./@Date"/>
</xsl:attribute>
</st>
</xsl:for-each>
</CList>
</xsl:for-each>
</ItemList>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


If you let me know what is the error I am making that would be really great,................

 
Old October 27th, 2007, 11:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Do try to learn how to use template rules and xsl:apply-templates. You can code XSLT without using these constructs, but it's a bit like riding a bicycle without using the gears.

There's presumably a Document element in your input that you haven't shown us? If there are multiple ItemList elements and you are trying to group within each one, then that's quite difficult using the Muenchian method, because Muenchian grouping relies on keys which work at document level.

I suspect that the thing that's stopping your code working is

<xsl:for-each select="//Item[generate-id(.) = generate-id(key('rows',Code)[1])]" >

the fact that Code is an attribute not an element, so it should be @Code.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 27th, 2007, 12:21 PM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the time and for the suggestions......

Document element is not a problem, that occurs only that is taken care.
If we consider ItemList is not grouping element (this can be neglected).
I have fixed the @code.

I have one more example to explain my question,

<ItemList>
    <Item Code="01234" Name="COLA">
      <List>
        <Number="104004" Code="u"/>
      </List>
      <CList>
        <st Level1="4535108" Date="2007-11-02" />
      </CList>
    </Item>
   <Item Code="01234" Name="COLA">
      <List>
        <Number="104004" Code="u"/>
      </List>
      <CList>
<st Level1="4535222" Date="2007-11-02" />
      </CList>
    </Item>
<Item Code="56789" Name="COLA">
      <List>
        <Number="204004" Code="u"/>
      </List>
      <CList>
<st Level1="33222" Date="2007-11-02" />
      </CList>
    </Item>
<Item Code="56789" Name="COLA">
      <List>
        <Number="204004" Code="u"/>
      </List>
      <CList>
<st Level1="55222" Date="2007-11-02" />
      </CList>
    </Item>

</ItemList>

The output should look like

<Item Code="01234" Name="COLA">
      <List>
        <Number="104004" Code="u"/>
      </List>
      <CList>
        <st Level1="4535108" Date="2007-11-02" />
       <st Level1="4535222" Date="2007-11-02" />
     </CList>
    </Item>

<Item Code="56789" Name="COLA">
      <List>
        <Number="204004" Code="u"/>
      </List>
      <CList>
        <st Level1="332222" Date="2007-11-02" />
       <st Level1="552222" Date="2007-11-02" />
     </CList>
    </Item>
 </ItemList>

This is the only logic I am having problem with, if the above output I am able to generated then I can fix the small small items.

Altered code
<?xml version="1.0"?>
<xsl:stylesheet exclude-result-prefixes="msxsl" version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:key name="rows" match="Item" use="@Code"/>

<xsl:template match="/">
<Document>
<xsl:call-template name="book"/>
</Document>
</xsl:template>

<xsl:template name="book">
<xsl:for-each select="//Document">
<xsl:attribute name="Name">
<xsl:value-of select="./@Name"/>
</xsl:attribute>
<ItemList>
<xsl:for-each select="//Item[generate-id(.) = generate-id(key('rows',@Code)[1])]" >
<Item>

<xsl:attribute name="Code">
<xsl:value-of select="./@Code"/>
</xsl:attribute>
<xsl:attribute name="Name">
<xsl:value-of select="./@Name"/>
</xsl:attribute>
</Item>

<List>
<xsl:for-each select="./List/Number">
<BList>
<xsl:attribute name="Number">
<xsl:value-of select="./@Number"/>
</xsl:attribute>
<xsl:attribute name="Code">
<xsl:value-of select="./@Code"/>
</xsl:attribute>
</BList>
</xsl:for-each>
</List>
<CList> <xsl:for-each select="./CList/st">
<st>
<xsl:attribute name="Level1">
<xsl:value-of select="./@Level1"/>
</xsl:attribute>
<xsl:attribute name="Date"><xsl:value-of select="./@Date"/>
</xsl:attribute>
</st>
</xsl:for-each>
</CList>
</xsl:for-each>
</ItemList>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Please give some tips.......

 
Old October 27th, 2007, 01:20 PM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It is working thanks................






Similar Threads
Thread Thread Starter Forum Replies Last Post
And/Or Logic??? ninel SQL Server 2000 2 February 9th, 2007 11:33 AM
Protect cells and allow grouping/un-grouping sfreuden Excel VBA 4 December 14th, 2006 08:01 AM
Tricky logic (for me) cedwards Dreamweaver (all versions) 5 April 5th, 2006 04:47 PM
typical logic rajanikrishna Classic ASP Basics 2 June 11th, 2004 11:15 PM
Mixing Data access logic and business logic polrtex BOOK: Professional Jakarta Struts 0 December 15th, 2003 07:19 PM





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