Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - Problems using the "following-sibling"-expression in XPATH

 
Thread Tools Search this Thread
Old 06-27-2003, 09:23 AM   #1
Default Problems using the "following-sibling"-expression in XPATH


Hi,

I'm using a style-sheet where I make use of the
XPATH-"following-sibling"-expression.

The part which makes problems looks similar to the following code:

---------------------------

<xsl:for-each select="headdata/extension/person">
<xsl:choose>
<xsl:when test="(position()) mod 3 = 1">
<tr>
<td>
<!-- Part 1 works -->
<xsl:text disable-output-escaping="yes">more info::</xsl:text>
<xsl:value-of select="funktion"/>

<!-- Part 2 works -->
<xsl:call-template name="handlePerson">
<xsl:with-param name="persondata" select="."/>
</xsl:call-template>
</td>

<td>
<!-- Part 3 works -->
<xsl:text disable-output-escaping="yes">more info:</xsl:text>
<xsl:value-of select="following-sibling::*[1]/funktion"/>

<!-- Part 4 doens'nt work-->
<xsl:call-template name="handlePerson">
<xsl:with-param name="persondata"
select="following-sibling::*[1]"/>
</xsl:call-template>
</td>

(...)
-----------------------------

I'm in the process of selecting a couple of persons and I want to give
the selected person to a sub-template which should do some processing.

I can access subfields (using following-sibling-expression) of the
current node (see Part 1).
I can access the current node and give it as parameter to the template
"handlePerson" and it works(see Part 2).

I can access subfields of the next sibling (see Part 3) and it works.

But I cannot give the next sibling as parameter to the template
"handlePerson" (see Part 4).

When giving the "following-sibling::*[1]" as parameter to the template
"handlePerson" it always processes the current node; the same with all
other following siblings.

Does anybody have an idea what might be wrong?

Thanks in advance

Peter Rohleder


Peter Rohleder
  Reply With Quote
Old 06-28-2003, 04:35 PM   #2
Peter Rohleder
 
Posts: n/a
Default Re: Problems using the "following-sibling"-expression in XPATH

Hi Marrow,

you write:

(...)
> Perhaps if you post some example XML and describe what you are actually
> trying to achieve? It is difficult to see what the problem might be from
> just an incomplete fragment of XSLT code.
>
> I suspect you might be wanting to organize things into a table with a fixed
> 3 coulumns?


yes, thats true!

I solved the problem by replacing following part, which doesn't work:

>>
>> <!-- Part 4 doens'nt work-->
>> <xsl:call-template name="handlePerson">
>> <xsl:with-param name="persondata"
>> select="following-sibling::*[1]"/>
>> </xsl:call-template>


(...)

by(new code):
-----------
<xsl:for-each select="following-sibling::*">
<xsl:if test="position() &lt; 3">

<td>
<xsl:call-template name="handlePerson">
<xsl:with-param name="persondata" select="."/> </xsl:call-template>
</td>

</xsl:if>
</xsl:for-each>
-----------

Now it works, but i still have no idea why the code above doesn't work
and the new code works.

Thanks for your response.

Peter Rohleder



  Reply With Quote
Old 06-28-2003, 05:04 PM   #3
Marrow
 
Posts: n/a
Default Re: Problems using the "following-sibling"-expression in XPATH

Hi Peter,

If you want to arrange something by a given number of fixed columns then
something like...

== XML =================================
<?xml version="1.0"?>
<headdata>
<extension>
<person>
<name>Aaaa</name>
<funktion>A1</funktion>
</person>
<person>
<name>Bbbb</name>
<funktion>B2</funktion>
</person>
<person>
<name>Cccc</name>
<funktion>C3</funktion>
</person>
<person>
<name>Dddd</name>
<funktion>D4</funktion>
</person>
<person>
<name>Eeee</name>
<funktion>E5</funktion>
</person>
</extension>
</headdata>
== end of XML ==========================

== XSL1 =================================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="html" indent="yes"/>
<xslaram name="no-cols" select="3"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:apply-templates select="headdata/extension/person[position()
mod $no-cols = 1 or $no-cols = 1]" mode="row-start"/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="person" mode="row-start">
<tr>
<xsl:apply-templates select=". | following-sibling:erson[position()
&lt; $no-cols]"/>
</tr>
</xsl:template>

<xsl:template match="person">
<td>
<xsl:value-of select="name"/>
<br/>
<xsl:text>more info: </xsl:text>
<xsl:value-of select="funktion"/>
</td>
</xsl:template>
</xsl:stylesheet>
== end of XSL1 ==========================

or...

== XSL2 =================================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="html" indent="yes"/>
<xslaram name="no-cols" select="3"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:for-each select="headdata/extension/person[position() mod
$no-cols = 1 or $no-cols = 1]">
<tr>
<xsl:for-each select=". | following-sibling:erson[position()
&lt; $no-cols]">
<td>
<xsl:value-of select="name"/>
<br/>
<xsl:text>more info: </xsl:text>
<xsl:value-of select="funktion"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>
== end of XSL2 ==========================

If your XML doesn't look like that then the XSL code might need changing -
but as you haven't posted any example XML that's something you will have to
figure.

The code will also need to be drastically different if you are sorting the
data as it is placed into the table - because axes work on the original
order of the nodes in the input document rather than the sorted node-set.

Cheers
Marrow


"Peter Rohleder" <> wrote in message
news:...
> Hi Marrow,
>
> you write:
>
> (...)
> > Perhaps if you post some example XML and describe what you are actually
> > trying to achieve? It is difficult to see what the problem might be

from
> > just an incomplete fragment of XSLT code.
> >
> > I suspect you might be wanting to organize things into a table with a

fixed
> > 3 coulumns?

>
> yes, thats true!
>
> I solved the problem by replacing following part, which doesn't work:
>
> >>
> >> <!-- Part 4 doens'nt work-->
> >> <xsl:call-template name="handlePerson">
> >> <xsl:with-param name="persondata"
> >> select="following-sibling::*[1]"/>
> >> </xsl:call-template>

>
> (...)
>
> by(new code):
> -----------
> <xsl:for-each select="following-sibling::*">
> <xsl:if test="position() &lt; 3">
>
> <td>
> <xsl:call-template name="handlePerson">
> <xsl:with-param name="persondata" select="."/> </xsl:call-template>
> </td>
>
> </xsl:if>
> </xsl:for-each>
> -----------
>
> Now it works, but i still have no idea why the code above doesn't work
> and the new code works.
>
> Thanks for your response.
>
> Peter Rohleder
>
>
>



  Reply With Quote
Old 07-02-2003, 02:29 PM   #4
C. M. Sperberg-McQueen
 
Posts: n/a
Default Re: Problems using the "following-sibling"-expression in XPATH

Peter Rohleder <> writes:

> I solved the problem by replacing following part, which doesn't work:
>
> >>
> >> <!-- Part 4 doens'nt work-->
> >> <xsl:call-template name="handlePerson">
> >> <xsl:with-param name="persondata"
> >> select="following-sibling::*[1]"/>
> >> </xsl:call-template>

>
> (...)
>
> by(new code):
> -----------
> <xsl:for-each select="following-sibling::*">
> <xsl:if test="position() &lt; 3">
>
> <td>
> <xsl:call-template name="handlePerson">
> <xsl:with-param name="persondata" select="."/>
> </xsl:call-template>
> </td>
>
> </xsl:if>
> </xsl:for-each>
> -----------
>
> Now it works, but i still have no idea why the code above doesn't work
> and the new code works.


It's hard to know for sure, since your original example was
fragmentary, but this sure looks like it may be a problem in the
template you are calling. When you pass in a node under the
name 'persondata', you have access in the template to two
conceptually distinct nodes: (1) the $persondata node, and (2)
the current node. If I had to guess, I'd suspect that the
template 'handlePerson' habitually refers to the current node
(i.e. '.') where it should refer to $persondata. Since in some
cases (the calls that work) the two nodes are identical, it
works sometimes, and makes the problem look as if it's a problem
in the call, rather than a problem in the template.

I hope this helps.

-C. M. Sperberg-McQueen
World Wide Web Consortium
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump