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

Re: looping problem, <xsl: for-each>??

Subject: Re: looping problem, <xsl: for-each>??
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 09 Oct 2003 16:36:41 -0400
xsl fo for each
Mukul,

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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan">
<xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>

<xsl:key name="x" match="/rss/channel/item"
use="category"/>
<xsl:template match="/">
<xsl:variable name="treeFrag">
<rootelem>
<xsl:for-each select="rss/channel/item">
<xsl:if test="generate-id(.) = generate-id(key('x',
category)[1])">
<h1> <xsl:value-of select="category"/>
</h1>
<xsl:for-each select="key('x', category)">
<desc>
<xsl:value-of select="description"/>
</desc>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</rootelem>
</xsl:variable>


  <xsl:for-each
select="xalan:nodeset($treeFrag)/rootelem/desc">
    <xsl:if test="name(preceding-sibling::*[1]) =
'h1'">
      <h1>
        <xsl:value-of select="preceding-sibling::*[1]" />
      </h1>
    </xsl:if>
    ITEM<xsl:value-of select="position()"/>
<xsl:value-of select="."/>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

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>


  <xsl:apply-templates
    select="xalan:nodeset($treeFrag)/rootelem" mode="treefrag">
  </xsl:for-each>
</xsl:template>

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


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

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

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).

Cheers,
Wendell


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 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: http://www.mulberrytech.com/xsl/xsl-list



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.