[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] XSLT 2.0 & Grouping for-each-group


Subject: Re: [xsl] XSLT 2.0 & Grouping for-each-group
From: Mark Brand <mark.brand@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 08 Aug 2003 22:35:11 +1000

* Michael Kay

<snip>	... with
	xsl:variable name="next" select="*[1]/@StyleName"
	xsl:for-each-group group-starting-with="*[@StyleName=$next]"
		This also has the advantage that it's the same code at all levels.
</snip>

* Mark Brand

Hi Michael

This change unless I have made an error in coding (but how hard can it be to cut and paste;-)) has
caused a problem it is only picking up the first Grouping (i think). The code after the change is
in Listing 1 below. Listing 2 is the input doc., Listing 3 is the output from the program prior to the $next change
and Listing 4 is the output after the $next change.


If you have any further suggestions ....

Thanks
Mark

=================================================================
Listing 1
<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="Document/DocumentBody">
<Regulation>
<xsl:variable name="next" select="*[1]/@StyleName"/>
<xsl:for-each-group select="Paragraph" group-starting-with="*[@StyleName=$next]">
<Part>
<xsl:copy-of select="child::node()"/> <xsl:variable name="next" select="*[1]/@StyleName"/>
<xsl:for-each-group select="current-group() except ." group-starting-with="*[@StyleName=$next]">
<Division>
<xsl:copy-of select="child::node()"/>
<xsl:variable name="next" select="*[1]/@StyleName"/>
<xsl:for-each-group select="current-group() except ." group-starting-with="*[@StyleName=$next]">
<Article>
<xsl:copy-of select="child::node()"/>
<xsl:variable name="next" select="*[1]/@StyleName"/>
<xsl:for-each-group select="current-group() except ." group-starting-with="*[@StyleName=$next]">
<Sub-Article>
<xsl:copy-of select="child::node()"/>
</Sub-Article> </xsl:for-each-group>
</Article> </xsl:for-each-group>
</Division>
</xsl:for-each-group>
</Part>
</xsl:for-each-group>
</Regulation>
</xsl:template>
</xsl:stylesheet>
=================================================================
Listing 2
<?xml version="1.0" encoding="ISO-8859-1"?>
<Document>
<DocumentBody>
<Paragraph StyleName="PART">
<Text>Part 1 Preliminary</Text>
</Paragraph>


       <Paragraph StyleName="DIVISION">
           <Text>Division 1.1    Introductory</Text>
       </Paragraph>

       <Paragraph StyleName="REGULATION">
           <Text>1.01 Name of <em>Regulations</em> [see Note 1]</Text>
       </Paragraph>

<Paragraph StyleName="SUB-REGULATION">
<Text> These Regulations are the Migration Regulations 1994</Text>
</Paragraph>
<Paragraph StyleName="SUB-REGULATION">
<Text> These Regulations are the Migration Regulations 1995</Text>
</Paragraph>


<Paragraph StyleName="REGULATION">
<Text>1.02 Commencement</Text>
</Paragraph>
<Paragraph StyleName="SUB-REGULATION">
<Text> These Regulations commence on 1 September 1994.</Text>
</Paragraph>


<Paragraph StyleName="SUB-REGULATION">
<Text> These Regulations commence on 1 September 1995.</Text>
</Paragraph>
<Paragraph StyleName="DIVISION">
<Text>Division 1.2 Interpretation</Text>
</Paragraph>
</DocumentBody>
</Document>
=================================================================
Listing 3
<?xml version="1.0" encoding="UTF-8"?>
<Regulation>
<Part>
<Text>Part 1 Preliminary</Text>
<Division>
<Text>Division 1.1 Introductory</Text>
<Article>
<Text>1.01 Name of <em>Regulations</em> [see Note 1]</Text>
<Sub-Article>
<Text> These Regulations are the Migration Regulations 1994</Text>
</Sub-Article>
<Sub-Article>
<Text> These Regulations are the Migration Regulations 1995</Text>
</Sub-Article>
</Article>
<Article>
<Text>1.02 Commencement</Text> <Sub-Article>
<Text> These Regulations commence on 1 September 1994.</Text>
</Sub-Article> <Sub-Article>
<Text> These Regulations commence on 1 September 1995.</Text>
</Sub-Article>
</Article>
</Division>
<Division>
<Text>Division 1.2 Interpretation</Text>
</Division>
</Part>
</Regulation>


=================================================================
Listing 4
<?xml version="1.0" encoding="UTF-8"?>
<Regulation>
<Part>
<Text>Part 1 Preliminary</Text>
<Division>
<Text>Division 1.1 Introductory</Text>
<Article>
<Text>1.01 Name of <em>Regulations</em> [see Note 1]</Text>
<Sub-Article>
<Text> These Regulations are the Migration Regulations 1994</Text>
</Sub-Article>
</Article>
</Division>
</Part>
</Regulation>
=================================================================
Michael Kay wrote:


Perhaps you could replace

xsl:for-each group group-starting-with="*[@StyleName='DIVISION']"

with
xsl:variable name="next" select="*[1]/@StyleName"
xsl:for-each-group group-starting-with="*[@StyleName=$next]"

This also has the advantage that it's the same code at all levels.

Michael Kay



-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Mark Brand
Sent: 07 August 2003 10:41
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] XSLT 2.0 & Grouping for-each-group - RESEND



* Micheal Kay
<snip>
But I may have misunderstood the requirement. What do you want to happen if there isn't a DIVISION item in the group? </snip>


* Mark Brand

Hi Michael

Thanks for the response, the incoming data will follow the hierachy below (Figure 1) and for this case the nested for-each-group works well.

But the only guarentee about the data is the order. It is not guarenteed that a particular level will be there. For example, a SUB-DIVISION level may be missing as per (Figure 2).

The nested for-each-group solution for the SUB-DIVISION level ignores the fact that there is no SUB-DIVISION entry in the group and processes the group entries anyway.

I thought I could get around this with a check on the for-each-group to see if (for example) the SUB-DIVISION level was in the current-group(), but that won't work because you still need to process the for-each-group because of the down level stuff.

Another issue is that, there are some other elements that are not part of the hierachy but can appear before or after any of the hierachial elements. For example (Figure 3) you may have a <NOTE> after a <DIVISION>

<PART>
	<DIVISION>
		<NOTE/>
		<REGULATION>
			<NOTE/>
		</REGULATION>
	</DIVISION>
</PART>


(Figure 1)
PART
DIVISION
SUBDIVISION
REGULATION
SUBREGULATION
PARAGRAPH
SUB-PARAGRAPH
SUB-SUB-PARAGRAPH


(Figure 2)
PART
DIVISION
REGULATION
SUBREGULATION
PARAGRAPH
SUB-PARAGRAPH
SUB-SUB-PARAGRAPH



(Figure 3)


<PART>
	<DIVISION>
		<NOTE/>
		<REGULATION>
			<NOTE/>
		</REGULATION>
	</DIVISION>
</PART>


Thanks Mark Brand




Michael Kay wrote:



* Mark Brand

Hi

I have further questions on the fragments posted earlier. I
did try implementing both options but had some questions ...


Q1.
<snip>
<xsl:for-each-group select="current-group() except "." group-starting-with="*[@StyleName='DIVISION']">
</snip>


With this line of code, i found that it would execute (enter
the for-each-group
loop) even if there wasn't a DIVISION item in the group. How do I stop the loop being entered if the group-starting-with entry is not in the list. I have tried an if statement after the loop has been entered but it is too late then for my purposes.




This reads to me like

<xsl:if test="current-group()/*[@StyleName='DIVISION']">
<xsl:for-each-group select="current-group() except "." group-starting-with="*[@StyleName='DIVISION']">
...
</xsl:if>


But I may have misunderstood the requirement. What do you want to happen if there isn't a DIVISION item in the group?





Q2.
<snip>
<Part Category="{@StyleName}">
  <xsl:copy-of select="child::node()"/>
  <xsl:for-each-group select="current-group() except ."
     group-starting-with="*[@StyleName=f:child(@StyleName)]">
    <xsl:apply-templates select="."/>
</snip>

With this piece of code from the second option where would
you put the closing tags, where-ever i put them they would all output after everything else instead of in a nested fashion.






Clearly the XSLT must be well-formed XML, so the closing

tags have to

be properly nested, and they will then also be properly

nested in the

result document. In fact, it's impossible to output a

document in which

the tags aren't properly nested! So I don't think I understand the question.

Michael Kay


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list






XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list





XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list





XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords