[xsl] for-each-group with multiple conditions(nodes)

Subject: [xsl] for-each-group with multiple conditions(nodes)
From: "Ramkumar" <ramkumar@xxxxxxxxxxxxxxxxxx>
Date: Thu, 16 Jul 2009 19:17:57 +0530
Hi List,

I am doing xslt transformation using v2.0 with saxon9 processor. I am not
able to achieve my expected output through for-each-group of multiple nodes
in a single document. Please look into my detailed inputs and find mistakes
in my below code.

$input xml
===============
<?xml version="1.0" encoding="utf-8"?>
<book xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/";>
<para aid:pstyle="LevelF">Comprehension</para>
<para aid:pstyle="LevelM">A	Answer these questions briefly.</para>
<para aid:pstyle="LevelM_sub">1.	Where did Monu live?</para>
<para aid:pstyle="LevelM_sub">2.	What did the baker make?</para>
<para aid:pstyle="LevelM">B	Read these sentences from the story.</para>
<para aid:pstyle="LevelM_sub">1.	&#x0201C;Thief!&#x0201D; he cried.
&#x0201C;You are stealing my smells.&#x0201D;</para>
<para aid:pstyle="LevelM_sub">2.	&#x0201C;Those smells come here on
the air. I don&#x00027;t steal them.&#x0201D;</para>
<para aid:pstyle="LevelM">C	Read these sentences. Say if these sentences
are true (T) or not true (NT).</para>
<para aid:pstyle="LevelM">B	Replace the words in colour with the number
given in the brackets. Adds to the noun and rewrite the sentence.</para>
<para aid:pstyle="LevelM_sub">1.	I saw an orange. (four)</para>
<para aid:pstyle="LevelF">Writing</para>
<para aid:pstyle="LevelM">A	Complete these sentences with words from the
box. You will then have a summary of the story.</para>
<para aid:pstyle="LevelM_sub">1.	Monu was a
___________________.</para>
<para aid:pstyle="LevelM_sub">2.	He lived in a ________ above the
baker&#x00027;s shop.</para>
</book>

$xsl2.0 with saxon9 processor
==============================
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/";
exclude-result-prefixes="aid">

<xsl:output method="xml" encoding="us-ascii" indent="yes" />

<xsl:template match="book">
 <book>
  <xsl:for-each-group select="*"
group-starting-with="para[contains(@aid:pstyle,'Level')]">
   <xsl:choose>
	    <xsl:when test="self::para/@aid:pstyle[contains(.,'LevelF')]">
	     <qandaset>
	      <xsl:call-template name="g">
	       <xsl:with-param name="e" select="current-group()"/>
	      </xsl:call-template>
	     </qandaset>
	    </xsl:when>
	    <xsl:when test="self::para/@aid:pstyle[contains(.,'LevelM')]">
	     <question>
	      <xsl:call-template name="g">
	       <xsl:with-param name="e" select="current-group()"/>
	      </xsl:call-template>
	     </question>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:call-template name="g">
	       <xsl:with-param name="e" select="current-group()"/>
	      </xsl:call-template>
	    </xsl:otherwise>
   </xsl:choose>
  </xsl:for-each-group>
 </book>
</xsl:template>

<xsl:template name="g">
 <xsl:param name="e"/>
 <xsl:for-each-group select="$e"
group-adjacent="string(self::para/@aid:pstyle)">
  <xsl:choose>
   <xsl:when test="self::para/@aid:pstyle[contains(.,'LevelF')]">
    <title>
     <xsl:apply-templates select="current-group()"/>
    </title>
   </xsl:when>
   <xsl:when test="self::para/@aid:pstyle[contains(.,'LevelM_sub')]">
    <para>
     <xsl:apply-templates select="current-group()"/>
    </para>
   </xsl:when>
   <xsl:otherwise>
    <xsl:apply-templates select="current-group()"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>

$translated output
==================
<?xml version="1.0" encoding="us-ascii"?>
<book>
   <qandaset>
      <title>Comprehension</title>
   </qandaset>
   <question>A	Answer these questions briefly.</question>
   <question>
      <para>1.	Where did Monu live?</para>
   </question>
   <question>
      <para>2.	What did the baker make?</para>
   </question>
   <question>B	Read these sentences from the story. Answer the questions
that follow.</question>
   <question>
      <para>1.	&#8220;Thief!&#8221; he cried. &#8220;You are stealing my
smells.&#8221;</para>
   </question>
   <question>
      <para>2.	&#8220;Those smells come here on the air. I don't steal
them.&#8221;</para>
   </question>
   <question>C	Read these sentences. Say if these sentences are true (T) or
not true (NT).</question>
   <question>B	Replace the words in colour with the number given in the
brackets. Adds to the noun and rewrite the sentence.</question>
   <question>
      <para>1.	I saw an orange. (four)</para>
   </question>
   <qandaset>
      <title>Writing</title>
   </qandaset>
   <question>A	Complete these sentences with words from the box. You will
then have a summary of the story.</question>
   <question>
      <para>1.	Monu was a ___________________.</para>
   </question>
   <question>
      <para>2.	He lived in a ________ above the baker's shop.</para>
   </question>
</book>

$expected output
=================
<?xml version="1.0" encoding="us-ascii"?>
<book>
   <qandaset>
     <title>Comprehension</title>
	   <question>A	Answer these questions briefly.
  	    <para>1.	Where did Monu live?</para>
	      <para>2.	What did the baker make?</para>
	   </question>
	   <question>B	Read these sentences from the story. Answer the
questions that follow.
      <para>1.	&#8220;Thief!&#8221; he cried. &#8220;You are stealing my
smells.&#8221;</para>
      <para>2.	&#8220;Those smells come here on the air. I don't steal
them.&#8221;</para>
   </question>
   <question>C	Read these sentences. Say if these sentences are true (T) or
not true (NT).</question>
   <question>B	Replace the words in colour with the number given in the
brackets. Adds to the noun and rewrite the sentence.
      <para>1.	I saw an orange. (four)</para>
   </question>
	</qandaset>
  <qandaset>
   <title>Writing</title>
   <question>A	Complete these sentences with words from the box. You will
then have a summary of the story.
      <para>1.	Monu was a ___________________.</para>
      <para>2.	He lived in a ________ above the baker's shop.</para>
   </question>
	</qandaset>
</book>

Regards,
Ramkumar,

Current Thread