[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Multiple for-each-group in a single template

Subject: Re: Multiple for-each-group in a single template
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 20 Nov 2008 12:17:28 GMT
Re:  Multiple for-each-group in a single template
> For Box and feature I am using "xsl:for-each-group group-adjacent"
> 
> But For section, I need "xsl:for-each-group group-starting-with" in the same
> template.


So you to need to nest those tests, the following xsl is a fairly direct
translation of your problem description.

> encoding="iso-8859-1"
> omit-xml-declaration="yes" />

Don't do that! XSLT2 will do what you ask, but it will mean that the
resulting file is not well formed if it contains any european accented
characters, or pound sign etc. Either use the default UTF8 encoding or
allow XSLT to add a declaration saying what encoding is used.
The only exception (and one I use often) is to say

encoding="US-ASCII"
omit-xml-declaration="yes" />


as that is a non-standard encoding but as it is compatiblie with utf8
you can and probably should omit the encoding declaration so that it can
be read with all xml parsers including any that might not support
US-ASCII.


David

$ saxon9 grp3.xml grp3.xsl
<chapter>
   <body>
      <section1>
         <p class="heading-1">Heading 1</p>
         <p>para 1</p>
         <box type="box" page="">
            <p class="box">para 2</p>
            <p class="box">para 3</p>
         </box>
         <p>para 4</p>
      </section1>
      <p class="heading-2">Heading 2</p>
      <p>para 1</p>
      <exercise>
         <p class="exercise">para 2</p>
         <p class="exercise">para 3</p>
      </exercise>
      <p>para 4</p>
   </body>
</chapter>





<html>
<body>
	<p class="heading-1">Heading 1</p>
		<p>para 1</p>
		<p class="box">para 2</p>
		<p class="box">para 3</p>
		<p>para 4</p>
	<p class="heading-2">Heading 2</p>
		<p>para 1</p>
		<p class="exercise">para 2</p>
		<p class="exercise">para 3</p>
		<p>para 4</p>
</body>
</html>

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" 
	    omit-xml-declaration="yes" />

<xsl:strip-space elements="*"/>

<xsl:template match="html">
 <chapter>
  <xsl:apply-templates/>
 </chapter>
</xsl:template>

<xsl:template match="*">
 <xsl:copy>
  <xsl:copy-of select="@*"/>
  <xsl:apply-templates/>
 </xsl:copy>
</xsl:template>

<xsl:template match="body">
 <body>
  <xsl:for-each-group select="*"
		      group-starting-with="p[contains(@class,'heading-')]">
   <xsl:choose>
	    <xsl:when
		test="self::p/@class[contains(.,'heading-1')]">
	     <section1>
	      <xsl:call-template name="g">
	       <xsl:with-param name="e" select="current-group()"/>
	      </xsl:call-template>
	     </section1>
	    </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>
 </body>
</xsl:template>
 


<xsl:template name="g">
 <xsl:param name="e"/>
 <xsl:for-each-group select="$e"
		     group-adjacent="string(self::p/@class)">
  <xsl:choose>
   <xsl:when test="self::p/@class[contains(.,'box')]">
    <box type="box"
	 page="{preceding-sibling::p[@class='pageNumber'][1]}">
     <xsl:apply-templates select="current-group()"/>
    </box>
   </xsl:when>
   <xsl:when test="self::p/@class[contains(.,'exercise')]">
    <exercise>
     <xsl:apply-templates  select="current-group()"/>
    </exercise>
   </xsl:when>
   <xsl:when test="self::p/@class[contains(.,'marginText')]">
    <margintext type="quotation">
     <xsl:apply-templates
	 select="current-group()"/>
    </margintext>
   </xsl:when>
   <xsl:otherwise>
    <xsl:apply-templates select="current-group()"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. 
________________________________________________________________________

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.