473,386 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

XSLT for-each position() problem

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:otherwise>
<xsl:value-of select=".//value"/>
</xsl:otherwise>
</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..

Feb 9 '07 #1
1 4713
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

Feb 9 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lisa | last post by:
I need to apply the HTML formatting tags and the French accented characters in a XML document. The XML is generated from a database that has HTML tags and French accented characters in the records....
0
by: Mike | last post by:
I'm generating an XSLT document programatically in VB.Net. I'm then trying to apply that XSLT against a cXML document to generate my own internally developed XML document. I'm using RichTextBox...
3
by: Jack Fox | last post by:
I've never had the need to work with XML, but I believe I now have an appropriate application. I have time-series data in objects organized as a tree that I want an ASP.NET program to write out to...
4
by: Moogy | last post by:
I'm pulling my hair out here. First, I'm new to XML, so that doesn't help, but none of this makes any sense to me. All I'm trying to do is take a simple source XML file and translate it with an...
2
by: Patrick J. Schouten | last post by:
I am trying to transfor an XML document prior to loading into a Dataset. My problem stems from the known bug in Visual Studio that prevents loading a well formed XML because of duplicate child...
3
by: thomas.porschberg | last post by:
Hi, I want to read records from a database and export it in an arbitrary format. My idea was to feed a class with a String array fetched from the database and let this class fire SAX events as...
4
by: Michal | last post by:
Hi everyone, Microsoft states that the System.XML 1.0 included in framework 1.1 supports the W3C XSLT Specification. However, I am having a problem using the document() fuction specified in...
3
by: RC | last post by:
Let's say: if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { // Now I got an XML object here var xmlDocument = XMLHttpRequestObject.responseXML; // next I have...
2
by: Piotr | last post by:
Hello, i got a problem with my hosting provider and the system admin seems not to be a top class specialist... I'm not one myself though. The problem is related to XSLT support in php....
0
by: ManWithNoName | last post by:
The following question is related to this thread: XSLT, document() function, filename, read non-standard/English characters (like µ) I have a XML file with a non-English character in its name...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.