xsl-list
[Top] [All Lists]

RE: Return position in for-each

2004-07-22 17:09:32
[Michael Kay Wrote]
Incidentally,

  <xsl:variable name="x"><xsl:value-of select="y"/></xsl:variable>

should in 99.99% of cases be rewritten

  <xsl:variable name="x" select="y"/>


Michael,
I thought I was writing these variables out correctly.  I believed that your
suggestion was for when you were passing XML document fragments around (not
sure how to express that properly), where y might be of the source:

<x>
  <y/>
  <y/>
  <y/>
</x>



About my logic:
You have XML source <ABC/> with 50 <Entry> elements (which begins to look
like this):
<ABC>
  <Entry>
        <Element name="name"/>
        <Element name="country"/>
        <Element name="phone"/>
        ... etc ...
  </Entry>
  <Entry>
        <Element name="name"/>
        <Element name="country"/>
        <Element name="phone"/>
        ... etc ...
  </Entry>
  ... etc (50 times) ...
</ABC

From this we make 50 sets of form input elements, each with the naming
convention:
        Element/@name + '_' + parent::/position()
(on another note, is it possible to get the position of the parent node like
that?)

User submits form... I have code which converts Form Vars to an XML source
called BROWSER_VARS.
Now I have a whole bunch of BROWSER_VARS, in the above case it would be 50 X
3, so 150 (which begins to look like this):

<VARS>
  <FORMVARS>
    <Element @name="name_1">Karl</Element>
    <Element @name="country_1">US</Element>
    <Element @name="phone_1">999-999-9999</Element>
    <Element @name="name_2">Michael</Element>
    <Element @name="country_2">US</Element>
    <Element @name="phone_2">999-999-9999</Element>
    ... etc ...
    <Element @name="name_1"></Element><!-- don't consider this one-->
  </FORMVARS>
  <QUERYVARS/>
</VARS>

So now, back to my <ABC/> source.  I look through this, using the existing
structure of this to support writing the browser variables back to the web
page and see where I have a matching BROWSER variable.  Basically, to
consider the browser variable set, there must be a value for the Element
@name.

(shoot, where was I going with this???)

Anyhow, does this help to understand the logic at all?  The ABC source is
like the driver... the BROWSER VARIABLES may ore may not exist.  Because of
the limitation of submitting form variables, there is no other way to build
my BROWSER VARIABLES source any other way than how it is (probably arguable,
but it is the way it is for now).

In English, the for-each loop would need to be:

Loop for (consider) each ABC/Entry where the corresponding
BROWSER_VARS//Element @name has a value...

HERE IS "WORK IN PROGRESS", if you are interested:
http://www.meetscoresonline.com/entry/

Thanks for the help!

Karl




Responsible for the following form elements:
<input name="name_1"/>

-----Original Message-----
From: Michael Kay [mailto:mhk(_at_)mhk(_dot_)me(_dot_)uk]
Sent: Thursday, July 22, 2004 4:15 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Return position in for-each


In general, move the "test" condition into a predicate in the select
expression of the xsl:for-each. Change

<xsl:for-each select="x">
  <xsl:if test="y">

to

<xsl:for-each select="x[y]">

In your example it's not quite so easy because you are defining a variable
before the <xsl:if>, and the variable itself accesses the position. I don't
understand your logic well enough at this time of night to suggest a
rewrite.

Incidentally,

  <xsl:variable name="x"><xsl:value-of select="y"/></xsl:variable>

should in 99.99% of cases be rewritten

  <xsl:variable name="x" select="y"/>

Michael Kay

-----Original Message-----
From: Karl J. Stubsjoen [mailto:karl(_at_)meetscoresonline(_dot_)com]
Sent: 22 July 2004 23:46
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Return position in for-each

Hello,

A disadvantage to an xsl:for-each is that the position()
increases by one
for each step.  so, if an xsl:if test validates the current
step and says
"Yes apply templates for this step", "don't apply templates
for this step",
and you are interested in a positiion which reflects the
total number of
"YES" steps... well you have a problem.  Take into consideration this
for-each rule:

<xsl:for-each select="Entry">
<xsl:variable name="element_name"><xsl:value-of
select="concat('competitor_name_',position())"/></xsl:variable>
  <xsl:if
test="string($BROWSER_VARS//ELEMENT[(_at_)name=$element_name]/.)">
    <tr>

      <!-- [[[ HERE WE ADD A COLUMN THAT HAS AN INCREMENTING
NUMBER ]]] -->
      <td><xsl:value-of select="concat(position(),'.')"/></td>

      <!-- [[[ HERE WE GET THE REST OF THE COLUMN DATA ]]] -->
      <xsl:apply-templates select="Element" mode="PRINTER_FRIENDLY">
         <xsl:with-param name="position"><xsl:value-of
select="position()"/></xsl:with-param>
      </xsl:apply-templates>
    </tr>
  </xsl:if>
</xsl:for-each>


Is there a work-around, or another way to accomplish an
incrementing number
for the TD?

Karl


--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--




--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--




<Prev in Thread] Current Thread [Next in Thread>