Problem with position()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with position()

Taras Bahnyuk

Let's say I have xml file

<?xml version="1.0" encoding="UTF-8"?>
<lists>
   <list1>
      <name filename="lalalla" storagename="1111"/>
      <name filename="lalalla" storagename="2222"/>
      <name filename="lalalla" storagename="3333"/>
      <name filename="lalalla" storagename="4444"/>
      <name filename="" storagename=""/>
      <name filename="lalala" storagename=""/>
   </list1>
   <list2>
      <name/>
      <name/>
      <name/>
      <name/>
      <name>5555</name>
      <name>6666</name>
   </list2>
</lists>

And I have an xsl stylesheet like

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="lists/list1/name[(@filename != '') and (@storagename ='') ]">
      <xsl:variable name="pos" select="position()"/>
      <name>
         <xsl:attribute name="storagename"><xsl:value-of select="/lists/list2/name[$pos]"/></xsl:attribute>
      </name>
   </xsl:template>

   <xsl:template match="name[@filename = '']">
   </xsl:template>
</xsl:stylesheet>


What I'm trying to achieve is to assight for each list1/name/@storagename the value from list2 that is at the same position().
And I'm expecting the result like

<?xml version="1.0" encoding="UTF-8"?>
<lists>
   <list1>
      <name filename="lalalla" storagename="1111" />
      <name filename="lalalla" storagename="2222" />
      <name filename="lalalla" storagename="3333" />
      <name filename="lalalla" storagename="4444" />
      <name xmlns:fo="http://www.w3.org/1999/XSL/Format" storagename="6666" />
   </list1>
   <list2>
      <name />
      <name />
      <name />
      <name />
      <name>5555</name>
      <name>6666</name>
   </list2>
</lists>

And XMLspy produces exactly the result I expect.
Orbeon though fails to produce it unless I change the line
<xsl:attribute name="storagename"><xsl:value-of select="/lists/list2/name[$pos]"/></xsl:attribute>
Of the stylesheet Into
<xsl:attribute name="storagename"><xsl:value-of select="/lists/list2/name[$pos div 2]"/></xsl:attribute>

Ok. I might live happily with the magic number ($pos div 2) but the problem is that in real application that number keeps changing depending on ... well, I'm not sure what :-)
Sometimes it requires ($pos div 2)+1, sometimes -1... This is pretty annoying, especially that this time I can't get my application to work.
I had tried previously mentioned snippets of code in XSLT Sandbox and this behaviour is reproducible, so I hope someone could point me towards a workaround.

Thank you very much.
Taras


-- The information contained in this communication and any attachments is confidential and may be privileged, and is for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender immediately by replying to this message and destroy all copies of this message and any attachments. ASML is neither liable for the proper and complete transmission of the information contained in this communication, nor for any delay in its receipt.

--
You receive this message as a subscriber of the [hidden email] mailing list.
To unsubscribe: mailto:[hidden email]
For general help: mailto:[hidden email]?subject=help
ObjectWeb mailing lists service home page: http://www.objectweb.org/wws
Reply | Threaded
Open this post in threaded view
|

Re: Problem with position()

Adrian Baker
Hi Taras,

The position() function isn't returning what you think it is: it gives
the position of the name node in the restricted list of names selected
by the
    lists/list1/name[(@filename != '') and (@storagename ='') ]
expression, *not* the position of the name node relative to all the name
nodes in the parent list1 element.

Something like

    <xsl:variable name="pos" select="count(preceding-sibling::name) + 1"/>

would work, there are probably other ways as well.

Adrian

Taras Bahnyuk wrote:

>
> Let's say I have xml file
>
> <?xml version="1.0" encoding="UTF-8"?>
> <lists>
>    <list1>
>       <name filename="lalalla" storagename="1111"/>
>       <name filename="lalalla" storagename="2222"/>
>       <name filename="lalalla" storagename="3333"/>
>       <name filename="lalalla" storagename="4444"/>
>       <name filename="" storagename=""/>
>       <name filename="lalala" storagename=""/>
>    </list1>
>    <list2>
>       <name/>
>       <name/>
>       <name/>
>       <name/>
>       <name>5555</name>
>       <name>6666</name>
>    </list2>
> </lists>
>
> And I have an xsl stylesheet like
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:fo="http://www.w3.org/1999/XSL/Format">
>    <xsl:template match="@*|node()">
>       <xsl:copy>
>          <xsl:apply-templates select="@*|node()"/>
>       </xsl:copy>
>    </xsl:template>
>
>    <xsl:template match="lists/list1/name[(@filename != '') and
> (@storagename ='') ]">
>       <xsl:variable name="pos" select="position()"/>
>       <name>
>          <xsl:attribute name="storagename"><xsl:value-of
> select="/lists/list2/name[$pos]"/></xsl:attribute>
>       </name>
>    </xsl:template>
>
>    <xsl:template match="name[@filename = '']">
>    </xsl:template>
> </xsl:stylesheet>
>
>
> What I'm trying to achieve is to assight for each
> list1/name/@storagename the value from list2 that is at the same
> position().
> And I'm expecting the result like
>
> <?xml version="1.0" encoding="UTF-8"?>
> <lists>
>    <list1>
>       <name filename="lalalla" storagename="1111" />
>       <name filename="lalalla" storagename="2222" />
>       <name filename="lalalla" storagename="3333" />
>       <name filename="lalalla" storagename="4444" />
>       <name xmlns:fo="http://www.w3.org/1999/XSL/Format"
> storagename="6666" />
>    </list1>
>    <list2>
>       <name />
>       <name />
>       <name />
>       <name />
>       <name>5555</name>
>       <name>6666</name>
>    </list2>
> </lists>
>
> And XMLspy produces exactly the result I expect.
> Orbeon though fails to produce it unless I change the line
> <xsl:attribute name="storagename"><xsl:value-of
> select="/lists/list2/name*[$pos*]"/></xsl:attribute>
> Of the stylesheet Into
> <xsl:attribute name="storagename"><xsl:value-of
> select="/lists/list2/name[*$pos div 2*]"/></xsl:attribute>
>
> Ok. I might live happily with the magic number ($pos div 2) but the
> problem is that in real application that number keeps changing
> depending on ... well, I'm not sure what :-)
> Sometimes it requires ($pos div 2)+1, sometimes -1... This is pretty
> annoying, especially that this time I can't get my application to work.
> I had tried previously mentioned snippets of code in XSLT Sandbox and
> this behaviour is reproducible, so I hope someone could point me
> towards a workaround.
>
> Thank you very much.
> Taras
>
>
> -- The information contained in this communication and any attachments
> is confidential and may be privileged, and is for the sole use of the
> intended recipient(s). Any unauthorized review, use, disclosure or
> distribution is prohibited. If you are not the intended recipient,
> please notify the sender immediately by replying to this message and
> destroy all copies of this message and any attachments. ASML is
> neither liable for the proper and complete transmission of the
> information contained in this communication, nor for any delay in its
> receipt.
> ------------------------------------------------------------------------
>
>
> --
> You receive this message as a subscriber of the [hidden email] mailing list.
> To unsubscribe: mailto:[hidden email]
> For general help: mailto:[hidden email]?subject=help
> ObjectWeb mailing lists service home page: http://www.objectweb.org/wws
>  



--
You receive this message as a subscriber of the [hidden email] mailing list.
To unsubscribe: mailto:[hidden email]
For general help: mailto:[hidden email]?subject=help
ObjectWeb mailing lists service home page: http://www.objectweb.org/wws
Reply | Threaded
Open this post in threaded view
|

RE: Problem with position()

Taras Bahnyuk
Adrian,

Thank's a lot. That helped :-)

Taras

-----Original Message-----
From: Adrian Baker [mailto:[hidden email]]
Sent: dinsdag 20 februari 2007 2:16
To: [hidden email]
Subject: Re: [ops-users] Problem with position()

Hi Taras,

The position() function isn't returning what you think it is: it gives
the position of the name node in the restricted list of names selected
by the
    lists/list1/name[(@filename != '') and (@storagename ='') ]
expression, *not* the position of the name node relative to all the name

nodes in the parent list1 element.

Something like

    <xsl:variable name="pos" select="count(preceding-sibling::name) +
1"/>

would work, there are probably other ways as well.

Adrian

--
The information contained in this communication and any attachments is confidential
and may be privileged, and is for the sole use of the intended recipient(s). Any
unauthorized review, use, disclosure or distribution is prohibited. If you are not
the intended recipient, please notify the sender immediately by replying to this
message and destroy all copies of this message and any attachments. ASML is neither
liable for the proper and complete transmission of the information contained in this
communication, nor for any delay in its receipt.





--
You receive this message as a subscriber of the [hidden email] mailing list.
To unsubscribe: mailto:[hidden email]
For general help: mailto:[hidden email]?subject=help
ObjectWeb mailing lists service home page: http://www.objectweb.org/wws