Project

Profile

Help

Grouping question

Added by Anonymous over 18 years ago

Legacy ID: #3792268 Legacy Poster: Dru_C (dru_c)

This question is related to using both "group-starting-with" and "group-adjacent" in the same template. At the end of this post is an example of the desired result. How can I make this happen? I suspect I'm missing the obvious. Thanks. *** THE XML **** <?xml version="1.0" encoding="UTF-8"?> <content> <part> <p type="h1"> H1 1 </p> <p type="body"> Body 1 - 1 </p> <p type="step"> Step 1 - 1 </p> <p type="step"> Step 1 - 2 </p> <p type="step"> Step 1 - 3 </p> <p type="body"> Body 1 - 1 </p> <p type="h1"> H1 2 </p> <p type="body"> Body 2 - 1 </p> <p type="step"> Step 2 - 1 </p> <p type="step"> Step 2 - 2 </p> <p type="step"> Step 2 - 3 </p> <p type="body"> Body 2 - 2 </p> </part> </content> *** THE XSL *** <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:template match="//part"> <xsl:for-each-group select="*" group-starting-with="p[@type='h1']"> <topic> <xsl:for-each select="current-group()"> <xsl:if test="normalize-space(@type) = 'h1'"> <xsl:element name="h1"> <xsl:apply-templates/> </xsl:element> </xsl:if> <xsl:if test="normalize-space(@type) = 'body'"> <xsl:element name="p"> <xsl:apply-templates/> </xsl:element> </xsl:if> <xsl:for-each-group select="." group-adjacent="@type='step'"> <xsl:if test="normalize-space(@type)='step'"> <xsl:element name="ul"> <xsl:for-each select="current-group()"> <xsl:element name="li"> <xsl:value-of select="."/> </xsl:element> </xsl:for-each> </xsl:element> </xsl:if> </xsl:for-each-group> </xsl:for-each> </topic> </xsl:for-each-group> </xsl:template> </xsl:stylesheet> *** THE RESULT *** <topic> <h1> H1 1 </h1> <p> Body 1 - 1 </p> <ul><li> Step 1 - 1 </li></ul> <ul><li> Step 1 - 2 </li></ul> <ul><li> Step 1 - 3 </li></ul> <p> Body 1 </p> </topic> <topic> <h1> H1 2 </h1> <p> Body 2 - 1 </p> <ul><li> Step 2 - 1 </li></ul> <ul><li> Step 2 - 2 </li></ul> <ul><li> Step 2 - 3 </li></ul> <p> Body 2 - 2 </p> </topic> *** THE DESIRED RESULT *** <topic> <h1> H1 1 </h1> <p> Body 1 - 1 </p> <ul> <li> Step 1 - 1 </li> <li> Step 1 - 2 </li> <li> Step 1 - 3 </li> </ul> <p> Body 1 </p> </topic> <topic> <h1> H1 2 </h1> <p> Body 2 - 1 </p> <ul> <li> Step 2 - 1 </li> <li> Step 2 - 2 </li> <li> Step 2 - 3 </li> </ul> <p> Body 2 - 2 </p> </topic>


Replies (2)

RE: Grouping question - Added by Anonymous over 18 years ago

Legacy ID: #3792612 Legacy Poster: Michael Kay (mhkay)

As this is a general XSLT coding question, rather than anything specific to Saxon, could I suggest that you ask it on the xsl-list at mulberrytech.com. I look at questions posted here if I have time, but I'm busy today. A quick comment, though: xsl:for-each-group select="." is clearly wrong; it doesn't make sense to do grouping on a sequence that only includes one item. Michael Kay Saxonica

RE: Grouping question - Added by Anonymous over 18 years ago

Legacy ID: #3793912 Legacy Poster: Dru_C (dru_c)

Thanks Michael, sorry for errant post.

    (1-2/2)

    Please register to reply