Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT for-each position() problem

Reply
Thread Tools

XSLT for-each position() problem

 
 
balderdash@gmail.com
Guest
Posts: n/a
 
      02-09-2007
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>
<xsltherwise>
<xsl:value-of select=".//value"/>
</xsltherwise>
</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..

 
Reply With Quote
 
 
 
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      02-09-2007
On Feb 9, 1:57 pm, balderd...@gmail.com wrote:
> 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.

--
Pavel Lepin

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Including XSLT/XML document within a XSLT document dar_imiro@hotmail.com XML 4 12-13-2005 02:26 AM
Multiple XSLT Transforms using a Controller XSLT sneill@mxlogic.com XML 2 10-19-2005 11:00 AM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
xslt alone or xslt/java for static site? ted XML 1 01-26-2004 10:41 AM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments