Altova Mailing List Archives

Re: [xsl] looping problem, ??

From: Wendell Piez <wapiez@---------------->
Date: 10/9/2003 1:04:00 PM

Nice job with this.

At 01:05 PM 10/9/2003, you wrote:
I applied your suggestion to store an intermediate
tree fragment in a variable, and then converting the
variable into a nodeset. By iterating the nodeset, I
am able to generate numbers 1,2,3,4..

The XSL is --

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
<xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>

<xsl:key name="x" match="/rss/channel/item"


<xsl:template match="/">

  <xsl:variable name="treeFrag">


      <xsl:for-each select="rss/channel/item">

        <xsl:if test="generate-id(.) = generate-id(key('x',


          <h1>                                              <xsl:value-of 


          <xsl:for-each select="key('x', category)">


              <xsl:value-of select="description"/>







    <xsl:if test="name(preceding-sibling::*[1]) =
        <xsl:value-of select="preceding-sibling::*[1]" />
    ITEM<xsl:value-of select="position()"/>
<xsl:value-of select="."/>


In the above solution, though numbers are getting
generated properly, but to output <h1> tags at the
right place, I had to write a slightly complex syntax
* xsl:if test="name(preceding-sibling::*[1]) = 'h1'" *

Yes. This could be improved by using plain old templates to process this 
stuff, as in:

<xsl:template match="/">

  <xsl:variable name="treeFrag"> [construct as you have 
it...]  </xsl:variable>

    select="xalan:nodeset($treeFrag)/rootelem" mode="treefrag">

<xsl:template match="item" mode="treefrag">

  ITEM<xsl:number level="any"/> <!-- I changed it to xsl:number: more 
dependable -->

  <xsl:value-of select="."/>


<xsl:template match="h1" mode="treefrag">
  <xsl:copy-of select="."/>

This can also be generalized further to handle a greater range of input 

I still think an XPath-based counting solution may be possible here, but 
it's highly dependent on the structure of the input (and hence, rather more 
brittle), and in the meantime I think I lost the input sample (it's around 
here somewhere).


Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

XSL-List info and archive:


These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.