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

RE: For-each loop or recursion

Subject: RE: For-each loop or recursion
From: "Aron Bock" <aronbock@xxxxxxxxxxx>
Date: Sat, 14 May 2005 03:22:37 +0000
sql for each
Oleg, first off, kudos on the thought you're bringing to bear on what you say is a new subject.


I have the following data (leaves in parallel branches):
mystruct/myarray1[i]/myvar/var2 and
mystruct/myarray2[i]/myvar/var3

I need to implement the find the first occurence of :
<xsl:if test="position() != last() and
  number(var2) = number(var2[position()+1]) and
  number(var3) = number(var3[position()+1])">
      <value-of select="position()">
</xsl:if>

You're approaching this procedurally, it seems, whereas you'd be better served using XSL's natural declarative model. Like with SQL, for example. Thus, rather than trying to pick through and find a specific node or value in imperative fashion, try to "describe" to the processor what you're looking for.


For example, with this XML input:

<mystruct>
   <myarray>
       <myvar>22</myvar>
       <myvar>2</myvar>
       <myvar>3</myvar>
       <myvar>5</myvar>
       <myvar>77</myvar>
       <myvar>78</myvar>
       <myvar>1</myvar>
   </myarray>
</mystruct>

This XSL will find the fist <myvar> node whose value is 1 less than it's immediately succeeding <myvar> sibling:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


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

<xsl:template match="/">
<node>
<xsl:copy-of select="mystruct/myarray/myvar[. = following-sibling::myvar[1] - 1][1]"/>
</node>
</xsl:template>
</xsl:stylesheet>


Yields:

<node>
 <myvar>2</myvar>
</node>

You should be able to do something similar for your problem.

Regards,

--A




A few questions to XSLT gurus: 1) Does it make sense to do in the for-each loop or it will require a recursion ? All I need is to get the position of the 1st occurrence. How would that recursion look like?

2) Will I be able to get a node from the parallel branch in for-each loop ?
Something like:
<xsl:for-each mystruct/myarray1[i]/myvar>
  <xsl:if test=" ...and number(../../myarray2[i]/myvar/var3) =
number((../../myarray2[i]/myvar/var3)[position()+1]) and...">
    <value-of select="position()">
  </xsl:if>
</xsl:for-each>)
I know it looks awful  :-(

3) Is there a way to somehow start the for-each loop
from position other than 1 ?
Like with j>1:
<xsl:for-each mystruct/myarray1[i]/myvar[j]>
....
</xsl:for-each>

I am using XSLT 1.x

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


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.