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 July 14th, 2010, 07:10 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default Grouping nodes by names

Hi, this question is related to:
grouping by nodes questions

After some thought, I rephrased my question and created an example.

Suppose I want to group elements by name and then put them into "groups". For example:

Input:
Code:
<data>
    <tools>Hammer</tools>
    <conditions>Sunny</conditions>
    <conditions>Dry</conditions>
    <tools>Saw</tools>
    <tools>Nail Gun</tools>
    <safety>Safety Glasses</safety>
    <conditions>Clear</conditions>
</data>
Desired output

Code:
<new>
    <group>
        <conditions>Clear</conditions>
        <conditions>Dry</conditions>
        <conditions>Sunny</conditions>
    </group>
    <group>
        <tools>Hammer</tools>
        <tools>Nail Gun</tools>
        <tools>Saw</tools>
    </group>
</new>
*Assume there can be an infinite number of children of <data>

This is a very simple example. In my actual document, I am processing this information into a FO file for PDF output. I am probably over thinking this one. I appreciate the help.

Thanks
 
Old July 14th, 2010, 07:15 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Code:
<new>
<xsl:for-each-group select="/data/*" group-by="name()">
  <group>
    <xsl:for-each select="current-group()">
      <xsl:element name="{name()}"><xsl:value-of select="."/></xsl:element>
    </xsl:for-each>
  </group>
</xsl:for-each-group>
</new>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
bonekrusher (July 14th, 2010)
 
Old July 14th, 2010, 07:20 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Assuming you still want to exclude safety elements, as you indicated earlier, and assuming you want to sort as well:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
  <xsl:output indent="yes"/>
  
  <xsl:template match="data">
    <new>
      <xsl:for-each-group select="*[not(self::safety)]" group-by="node-name(.)">
        <xsl:sort select="local-name-from-QName(current-grouping-key())"/>
        <group>
          <xsl:for-each select="current-group()">
            <xsl:sort select="string()"/>
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </group>
      </xsl:for-each-group>
    </new>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
bonekrusher (July 14th, 2010)
 
Old July 14th, 2010, 09:01 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Awesome - you guys are the best. Thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
grouping by nodes questions bonekrusher XSLT 4 July 13th, 2010 10:58 AM
Get distinct values on *second* grouping of nodes. MissHenesy XSLT 4 October 23rd, 2006 10:34 AM
Grouping orphan nodes to paranet elements ROCXY XSLT 2 January 1st, 2006 07:11 AM
Grouping stand alone nodes inside an element Bala007 XSLT 2 December 30th, 2005 02:25 PM
Grouping Nodes shoei78 XSLT 3 May 28th, 2004 10:34 PM





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