Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > BOOK: XSLT Programmer's Reference, 2nd Edition
|
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 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 December 1st, 2013, 09:54 AM
Registered User
 
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.
 
Old December 1st, 2013, 02:09 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
The Following User Says Thank You to mhkay For This Useful Post:
indigoXML (December 2nd, 2013)
 
Old December 2nd, 2013, 09:16 AM
Registered User
 
Join Date: Dec 2013
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

It does indeed, thank you (book bought!)





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 06: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 06:23 AM





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