Re: [xsl] counting with position()

Subject: Re: [xsl] counting with position()
From: "Imsieke, Gerrit, le-tex" <gerrit.imsieke@xxxxxxxxx>
Date: Tue, 05 Jan 2010 23:57:05 +0100
for-each-group is the answer to almost anything (next to 42, of course).

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:saxon="http://saxon.sf.net/";
  version="2.0"
  exclude-result-prefixes="saxon">

  <xsl:output
    method="xml"
    indent="yes"
    saxon:suppress-indentation="product"
    />


<xsl:template match="*[starts-with(name(), 'level')]">
<xsl:variable name="level-name" select="name()" />
<xsl:for-each-group select="*" group-starting-with="item">
<xsl:choose>
<xsl:when test="current-group()[1]/self::item">
<xsl:element name="{replace($level-name, 'level', 'step')}">
<text>
<para>
<xsl:apply-templates select="current-group()[1]/node()" />
</para>
</text>
<xsl:apply-templates select="current-group()[position() gt 1]" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<warning>
<para>There should be an item in the source at this point.</para>
</warning>
<!-- <xsl:copy-of select="current-group()" /> -->
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>


<xsl:template match="product[not(name(preceding-sibling::*[1]) = ('item', 'product'))]">
<warning>
<para>Dangling product in the source: <xsl:value-of select="."/> </para>
</warning>
</xsl:template>


  <xsl:template match="@* | *" priority="-1">
    <xsl:copy>
      <xsl:apply-templates select="@* | * | text()" mode="#current" />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>



a kusa schrieb:
My output must look like this:

<step1>
			<text>
				<para/>
			</text>
<product>sample prod1</product>
<product>sample prod2</product>

			<step2 id="idlistitem_2_30">
				<text>
					<para>Sample</para>
				</text>
<product>sample prod11</product>
<product>sample prod12</product>
<product>sample prod13</product>
				<step3>
					<text>
						<para>Sample</para>
					</text>
<product>sample prod14</product>
				</step3>
				<step3>
					<text>
						<para>Sample</para>
					</text>
				</step3>
			</step2>
			<step2 id="idlistitem_2_43">
				<text>
					<para/>
				</text>
<product>sample prod15</product>
<product>sample prod16</product>
				<step3>
					<text>
						<para>Sample</para>
					</text>
				</step3>
			</step2>
		</step1>



On Tue, Jan 5, 2010 at 4:18 PM, Imsieke, Gerrit, le-tex
<gerrit.imsieke@xxxxxxxxx> wrote:
I only want to match sample prod1 and sample prod2 after <item> at level1.
Please sketch the desired output to this input (as XML, not prose).

a kusa schrieb:
So my input can look like this:

<level1>
<item>Sample</item>
<product>sample prod1</product>
<product>sample prod2</product>
       <level2>
<item>Sample</item>
<product>sample prod11</product>
<product>sample prod12</product>
<product>sample prod13</product>
             <level3>
                <item>Sample</item>
<product>sample prod14</product>

                <item>Sample</item>
              </level3>
<item>Sample</item>
<product>sample prod15</product>
<product>sample prod16</product>
<item>Sample</item>
<level3>
<item>Sample</item>
</level3>
<product>sample prod17</product>

        </level2>
<level1>


-- Gerrit Imsieke Geschdftsf|hrer / Managing Director le-tex publishing services GmbH Weissenfelser Str. 84, 04229 Leipzig, Germany Phone +49 341 355356 110, Fax +49 341 355356 510 gerrit.imsieke@xxxxxxxxx, http://www.le-tex.de

Registergericht / Commercial Register: Amtsgericht Leipzig
Registernummer / Registration Number: HRB 24930

Geschdftsf|hrer: Gerrit Imsieke, Svea Jelonek,
Thomas Schmidt, Dr. Reinhard Vvckler

Current Thread