xsl-list
[Top] [All Lists]

[xsl] for-each-group for multiple nodes

2009-07-17 05:33:08
Hi List,

I am using xslt v2.0 with saxon9 processor.

I am not able to achieve my expected output through for-each-group for
multiple nodes in a single template match. 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>

xslt
=====
<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>

Transformed 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



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>