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 | 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 November 17th, 2006, 05:33 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trying to group child nodes

I have been attempting to group child nodes using the Munechian method but am now stumped.

I am trying to convert this XML
<PublisherList>
  <publisher>
    <name>Penguin</name>
    <books>
      <book>
        <title>Title One</title>
        <author>David</author>
      </book>
      <book>
        <title>Title Two</title>
        <author>John</author>
      </book>
      <book>
        <title>Title Three</title>
        <author>John</author>
      </book>
      <book>
        <title>Title Four</title>
        <author>David</author>
      </book>
    </books>
  </publisher>
  <publisher>
    <name>Wrox</name>
    <books>
      <book>
        <title>Title Five</title>
        <author>David</author>
      </book>
      <book>
        <title>Title Six</title>
        <author>John</author>
      </book>
      <book>
        <title>Title Seven</title>
        <author>John</author>
      </book>
      <book>
        <title>Title Eight</title>
        <author>David</author>
      </book>
    </books>
  </publisher>
</PublisherList>

To this:
<List>
    <BookPublisher>
        <name>Penguin</name>
        <BooksByAuthor>
            <AuthorName>David</AuthorName>
            <title>Title One</title>
            <title>Title Four</title>
        </BooksByAuthor>
        <BooksByAuthor>
            <AuthorName>John</AuthorName>
            <title>Title Two</title>
            <title>Title Three</title>
        </BooksByAuthor>
    </BookPublisher>
    <BookPublisher>
        <name>Wrox</name>
        <BooksByAuthor>
            <AuthorName>David</AuthorName>
            <title>Title Five</title>
            <title>Title Eight</title>
        </BooksByAuthor>
        <BooksByAuthor>
            <AuthorName>John</AuthorName>
            <title>Title Six</title>
            <title>Title Seven</title>
        </BooksByAuthor>
    </BookPublisher>
</List>

using this XSLT (so far)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml"/>

    <xsl:key name="publishers" match="publisher" use="name" />
    <xsl:key name="authors" match="author" use="." />

    <xsl:template match="/">
        <xsl:element name="List">

            <xsl:for-each select="//publisher[count(. | key('publishers', name)[1]) = 1]">
                <BookPublisher>
                    <xsl:variable name="publisher" select="name"/>
                    <name><xsl:value-of select="$publisher"/></name>

                <xsl:for-each select=".//author[count(.| key('authors',.)[1])=1]">
                    <AuthorName><xsl:value-of select="."/></AuthorName>
                </xsl:for-each>
                </BookPublisher>
            </xsl:for-each>

        </xsl:element>

    </xsl:template>

</xsl:stylesheet>


But I get this result:
<?xml version="1.0"?>
<List>
    <BookPublisher>
        <name>Penguin</name>
        <AuthorName>David</AuthorName>
        <AuthorName>John</AuthorName>
    </BookPublisher>
    <BookPublisher>
        <name>Wrox</name>
    </BookPublisher>
</List>

Can anyone explain why on the second pass through the for-each statment, the AuthorNames are not being extracted that are unique for Wrox. The only way I can make it work is by changing the authors key --> <xsl:key name="authors" match="publisher[name='Wrox']//author" use="." />

But I can't use a variable (name = $name)

Any help or insight would be appreciated

Reply With Quote
  #2 (permalink)  
Old November 17th, 2006, 08:21 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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm not sure why you're looking for distinct publishers, you don't have any duplicates in your data, and I'm not sure how you would want to handle them if there were any.

Using Muenchian grouping within a subtree, rather than within the full document, is always a little bit tricky. The usual answer is to define a key that makes the name globally unique, in this case concatenating the publisher name and the author name.

Where it's going wrong is here

<xsl:for-each select=".//author[count(.| key('authors',.)[1])=1]">

where you look for authors within the current publisher, and then test them to see if they are the first author with that name in the document. Try changing the key on author use="concat(ancestor::publisher/name, '#', .)", and changing the above to

select=".//author[count(.| key('authors',concat(current()/name, '#', .)[1])=1]">

I don't guarantee that's enough to make it work but it's the essence of the solution.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old November 18th, 2006, 11:27 AM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thats great! Thanks. I can't quite get it to work (its telling me that the expression, select=".//author[count(.| key('authors', concat(current()/name, '#', .)[1]=1)]" must evaluate to a node-set). I'm using MSXML 4.0 and can't seem to fit the node-set extension in the right placem but I think I can find my way now

Thanks so much for the insight!

Reply With Quote
  #4 (permalink)  
Old November 18th, 2006, 01:29 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it working! Thanks. I needed to change current() to a variable, but now its perfect. Thanks again

final version that works (now I can add the rest)

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

    <xsl:output method="xml"/>
    <xsl:key name="authors" match="author" use="concat(ancestor::publisher/name, '#', .)" />
    <xsl:template match="/">
        <xsl:element name="List">
            <xsl:for-each select="//publisher">
                <xsl:variable name="publisher" select="name"/>
        <p><xsl:value-of select="$publisher"/></p>
                <xsl:for-each select=".//author[count(.| key('authors', concat($publisher, '#', .))[1])=1]">
                 <b><xsl:value-of select="."/></b>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
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
How to get the size of the child nodes. eruditionist XSLT 2 November 7th, 2008 11:59 AM
not able to print out the child nodes eruditionist XSLT 7 October 30th, 2008 10:33 AM
text and child nodes eepyoga XSLT 1 April 8th, 2008 12:45 PM
Retrieveal of child nodes.... nathilson21 Classic ASP XML 0 May 7th, 2007 04:54 AM
counting child nodes Tomi XSLT 1 September 6th, 2006 03:26 AM



All times are GMT -4. The time now is 11:58 AM.


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