XSLT for-each position() problem

Discussion in 'XML' started by balderdash@gmail.com, Feb 9, 2007.

  1. Guest

    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..
    , Feb 9, 2007
    #1
    1. Advertising

  2. Guest

    On Feb 9, 1:57 pm, 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, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    396
    Eliyahu Goldin
    Aug 3, 2005
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    416
    Tjerk Wolterink
    Nov 3, 2004
  3. R
    Replies:
    2
    Views:
    3,307
    Ben LamHang
    Mar 18, 2005
  4. crabsdf
    Replies:
    0
    Views:
    751
    crabsdf
    Apr 29, 2008
  5. Pat Maddox
    Replies:
    6
    Views:
    141
    Marcin Mielżyński
    Jan 20, 2006
Loading...

Share This Page