XSLT for-each position() problem

B

balderdash

Hi

I am very close to achieving the output I need but I cant seem to get
it right.

The problem is I am looping through a table and selecting values, if
there are 2 values per (row) Issuer I need to put a slash between them
in the output xml.

The problem is shown by the output at the bottom of this post. I am
using position() to determine that a slash is needed however sometimes
the first value is empty and so position() is not good enough (as
below)

Since there is no way to store local variables in xslt nor can I find
a way to examine what has been previously selected I am stumped!

Is there a way to put the string-length check in the for-each select
statement??


Here is the code I have:

....
<xsl:variable name="dataValue">
<xsl:for-each select="$records[row=$CurrentRow]">
<xsl:sort select=".//col"/>

<xsl:if test="string-length(.//value) &gt; 1">
<xsl:choose>
<xsl:when test="position() &gt; 1">
<xsl:value-of select="$gSlash"/>
<xsl:value-of select=".//value"/>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select=".//value"/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:if>

</xsl:for-each>
</xsl:variable>
.....



Here is the slightly wrong output with the unwanted slashes:

<Issuer>
<Value>+0.9%/+0.5%</Value>
</Issuer>
<Issuer>
<Value>/8.9%</Value>
</Issuer>
<Issuer>
<Value>/-6.4%</Value>
</Issuer>


Any help appreciated..
 
P

p.lepin

I am very close to achieving the output I need but I cant
seem to get it right.

If you want help with your code, it's usually a good idea
to provide a minimum complete example that demonstrates
your problem. Since you didn't do that, all you get are my
post-lunch WAGs.
The problem is I am looping through a table and selecting
values, if there are 2 values per (row) Issuer I need to
put a slash between them in the output xml.

Since you didn't provide a sample of your source document,
it's hard to be sure what you're talking about, but you're
not 'looping' over anything in the piece of code you've
shown us. For-each is not a loop. There's no guaranteed
order-of-execution.
The problem is shown by the output at the bottom of this
post. I am using position() to determine that a slash is
needed however sometimes the first value is empty and so
position() is not good enough (as below)

The problem might be shown, or it might be not. It's hard
to tell since there isn't anything I could stuff into my
XSLT processor and take a look at how it actually works.
Since there is no way to store local variables in xslt
nor can I find a way to examine what has been previously
selected I am stumped!

Obviously. Because, well, for-each is not a loop. You
cannot examine what was selected 'previously' because
there's no guaranteed 'previously'. The nodes might be
processed simultaneously on different CPU cores for all you
know.
Is there a way to put the string-length check in the
for-each select statement??

There is.
<xsl:variable name="dataValue">
<xsl:for-each
select="$records[row=$CurrentRow]">
<xsl:sort select=".//col"/>
<xsl:if
test="string-length(.//value) &gt; 1">

<xsl:for-each
select=
"
$records
[row=$CurrentRow][string-length(.//value) &gt; 1]
">

Apart from the fact that for-each is evil and should be
avoided unless you know what it really does and why you
want it, this might help you.
From your code it's fairly obvious that you attempt to use
XSLT in the same way as whatever imperative language you've
used before it. This is a dead end. XSLT is most
emphatically not an imperative language. If you want to
achieve anything without stumbling on every other step, I
would heartily recommending spending a day or two
understanding how XSLT really works, and how to formulate
your problems in terms of templates, selects and matches,
not in terms of 'loops' and ifs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top