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

December 1st, 2013, 09:54 AM
|
Registered User
|
|
Join Date: Dec 2013
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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.
|

December 1st, 2013, 02:09 PM
|
 |
Wrox Author
Points: 18,438, Level: 59 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
|
|
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:
|
|

December 2nd, 2013, 09:16 AM
|
Registered User
|
|
Join Date: Dec 2013
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
It does indeed, thank you (book bought!)
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |