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 October 26th, 2007, 06:18 PM
Authorized User
Points: 63, Level: 1
Points: 63, Level: 1 Points: 63, Level: 1 Points: 63, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2007
Location: , , .
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.





Reply With Quote
  #2 (permalink)  
Old October 26th, 2007, 07:01 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
  #3 (permalink)  
Old October 26th, 2007, 10:08 PM
Authorized User
Points: 63, Level: 1
Points: 63, Level: 1 Points: 63, Level: 1 Points: 63, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2007
Location: , , .
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

Reply With Quote
  #4 (permalink)  
Old October 27th, 2007, 03:17 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
  #5 (permalink)  
Old October 27th, 2007, 05:21 AM
Authorized User
Points: 63, Level: 1
Points: 63, Level: 1 Points: 63, Level: 1 Points: 63, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2007
Location: , , .
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,................

Reply With Quote
  #6 (permalink)  
Old October 27th, 2007, 11:13 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
  #7 (permalink)  
Old October 27th, 2007, 12:21 PM
Authorized User
Points: 63, Level: 1
Points: 63, Level: 1 Points: 63, Level: 1 Points: 63, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2007
Location: , , .
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.......

Reply With Quote
  #8 (permalink)  
Old October 27th, 2007, 01:20 PM
Authorized User
Points: 63, Level: 1
Points: 63, Level: 1 Points: 63, Level: 1 Points: 63, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

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
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



All times are GMT -4. The time now is 08:53 PM.


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