Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > BOOK: XSLT Programmer's Reference, 2nd Edition
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: XSLT Programmer's Reference, 2nd Edition
This is the forum to discuss the Wrox book XSLT: Programmer's Reference, 2nd Edition by Michael Kay; ISBN: 9780764543814
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: XSLT Programmer's Reference, 2nd Edition 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 Display Modes
  #1 (permalink)  
Old December 1st, 2013, 08:54 AM
Registered User
Points: 20, Level: 1
Points: 20, Level: 1 Points: 20, Level: 1 Points: 20, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2013
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default Grouping within arbitrary document xml

Hi

I have some document-style xml which has an arbitrary structure. The problem is to create a group element around specific clusters of elements.
For example, say I'd like to group dog, cat and bird elements inside a new pets element but those elements arent always in the same location in the xml, such as the following case (sorry the indentation seems to get lost here):

<code>
<section>
<section>
<dog></dog>
<cat></cat>
<bird></bird>
<cow></cow>
</section>
<para>
<dog></dog>
<cat></cat>
<bird></bird>
<sub-para>
<cat></cat>
<bird></bird>
</sub-para>
</para>
</section>
</code>

How do I group such that a dog is always the start of a pets element and any following cat or bird (in any order or repetition) are included. Other elements (e.g. cow) are not to be included and stop the grouping. Critically cat or bird elements NOT preceded by a dog do not form a group (as in sub-para/cat above.

Here is the desired output:

<code>
<section>
<section>
<pets>
<dog></dog>
<cat></cat>
<bird></bird>
</pets>
<cow></cow>
</section>
<para>
<pets>
<dog></dog>
<cat></cat>
<bird></bird>
</pets>
<sub-para>
<cat></cat>
<bird></bird>
</sub-para>
</para>
</section>
</code>

With a flatter, more database style xml, this grouping might be easier. What complicates this problem (to my current knowledge of xslt 2) is the need to parse through the xml as a document rather than just grabbing and reordering nodes from a 'data' file. Second, the arbitrary document structure (adjacent dog, cat and bird elements could occur anywhere in the document) means there's no obvious parent element from which to apply a for-each-group and finally, the fact that the focus elements arent expressed in regular order.

If anyone can help, I'd appreciate it. One solution I've considered is doing 2 passes and tagging candidate elements before returning in
a second pass that groups consecutive elements that share the same tag. But I'm not sure where to issue a group-adjacent instruction from in that case.
Reply With Quote
  #2 (permalink)  
Old December 1st, 2013, 01:09 PM
mhkay's Avatar
Wrox Author
Points: 17,682, Level: 57
Points: 17,682, Level: 57 Points: 17,682, Level: 57 Points: 17,682, Level: 57
Activity: 22%
Activity: 22% Activity: 22% Activity: 22%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,825
Thanks: 0
Thanked 263 Times in 258 Posts
Default

If I understand it correctly, the following should do the job:

Code:
<xsl:template match="*">
<xsl:copy>
  <xsl:for-each-group select="*" group-adjacent="self::dog or self::cat or self::bird">
    <xsl:choose>
       <xsl:when test="current-grouping-key()">
          <pets><xsl:copy-of select="current-group()"/></pets>
       </xsl:when>
       <xsl:otherwise>
           <xsl:apply-templates select="current-group()"/>
       </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each-group>
</xsl:copy>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
indigoXML (December 2nd, 2013)
  #3 (permalink)  
Old December 2nd, 2013, 08:16 AM
Registered User
Points: 20, Level: 1
Points: 20, Level: 1 Points: 20, Level: 1 Points: 20, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2013
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

It does indeed, thank you (book bought!)
Reply With Quote
Reply


Thread Tools
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
Split xml file with result document and javax.xml.transform.Transformer. nisargmca XSLT 3 January 12th, 2010 05:26 AM
grouping and result-document help mrame XSLT 2 October 21st, 2008 11:01 PM
XML Grouping Problem andymidd XSLT 2 July 10th, 2007 02:48 AM
Formating arbitrary data into a table. Axiverse XSLT 1 July 2nd, 2007 05:40 PM
Print arbitrary text Joachim Hofmann SQL Language 0 November 14th, 2005 05:23 AM



All times are GMT -4. The time now is 03:20 PM.


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