RE: [xsl] How to output open/close tags independently?

Subject: RE: [xsl] How to output open/close tags independently?
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Fri, 27 Dec 2002 10:34:12 -0500
[ Mitch Amiano]

> I don't see what the connection is between trying to coerce 
> XSLT into accepting non-well-formed markup, and grouping. As 
> others have pointed out, the FAQ gives a good example of how 
> to group by a fixed number of elements.  For grins, I took 
> your sample and modified it slightly: <?xml version="1.0"?> 
> <batchup> <x>0</x> <x>1</x> <x>2</x> <x>3</x> <x>4</x> 
> <x>5</x> <x>6</x> <x>7</x> <x>8</x> <x>9</x> <x>0</x> 
> <x>1</x> <x>2</x>
> 
> ... and so on, for 1000 <x> elements.
> 

> Then I wrote a small stylesheet based on the FAQ method:
>

Look, the problem as stated is a really easy problem, and it does not
even require recursion.  As I have said many times, these problems are
mostly a matter of figuring out how to select the right nodes.  In this
case, the task was said to be to wrap every n of the "x" elements inside
a "w" element.  

This requires simple selection.  It does not require recursion.  It
absolutely does not require "trying to coerce  XSLT into accepting
non-well-formed markup"  It is a form of grouping, but much simpler than
the grouping examples that standard FAQ solutions address.

An simple solution (it is even parameterized) is this -

=========== stylesheet ========================
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output encoding='iso-8859-1' />

<xsl:variable name='modulus' select='number("4")'/>
<xsl:variable name='first-elements-in-container' 
	select='/root/x[position() mod $modulus = 1]'/>

<xsl:template match="/root">
<results>
	<xsl:apply-templates select='$first-elements-in-container'/>
</results>
</xsl:template>

<xsl:template match='x'>
<w>
	<xsl:copy-of select='.'/>
	<xsl:copy-of select='following-sibling::x[$modulus >
position()]'/>
</w>
</xsl:template>

</xsl:stylesheet>

================= input ==============
<root>
<x>1</x>
<x>2</x>
<x>3</x>
<x>4</x>
<x>5</x>
<x>6</x>
<x>7</x>
<x>8</x>
<x>9</x>
<x>10</x>
<x>11</x>
<x>12</x>
<x>13</x>
<x>14</x>
<x>15</x>
<x>16</x>
<x>17</x>
<x>18</x>
<x>19</x>
<x>20</x>
<x>21</x>
<x>22</x>
</root>

============== result =================
<results>
<w>
  <x>1</x> 
  <x>2</x> 
  <x>3</x> 
  <x>4</x> 
</w>
<w>
  <x>5</x> 
  <x>6</x> 
  <x>7</x> 
  <x>8</x> 
</w>
<w>
  <x>9</x> 
  <x>10</x> 
  <x>11</x> 
  <x>12</x> 
</w>
<w>
  <x>13</x> 
  <x>14</x> 
  <x>15</x> 
  <x>16</x> 
</w>
<w>
  <x>17</x> 
  <x>18</x> 
  <x>19</x> 
  <x>20</x> 
</w>
<w>
  <x>21</x> 
  <x>22</x> 
</w>
</results>


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


Current Thread