xsl-list
[Top] [All Lists]

Re: XSL Problem

2004-08-11 13:10:41
Sorry, I should have included output in my previous message... 

When I run the following:

 <xsl:template match="/root">
   <twos>
     <xsl:copy-of select="one/two[(_at_)in][1]"/>
   </twos>
 </xsl:template>

against the provided xml I get this:
<twos>
  <two in="1">2.1</two>
  <two in="2">2.1</two>
  <two in="3">2.1</two>
</twos>

which is the first "two" node from each "one" node.

if I remove the "in" attribute from the first "two" node, I get this:

<twos>
  <two in="1">2.2</two>
  <two in="2">2.1</two>
  <two in="3">2.1</two>
</twos>

which is what I would expect given the behavior of the previous run.

The behavior I was expecting can be replicated with this statement:
<xsl:copy-of select="(one/two)[(_at_)in][1]"/>
or
<xsl:copy-of select="(one/two[(_at_)in])[1]"/>

I think I understand now why this is happening. I'll write it down to
make sure that I have it straight. Each step in the location path
selects a node-set, that node-set is then evaluated against the rest
of the steps in the location path. I have been thinking of the
location steps like a sieve, where each step filtered out unwanted
nodes, and the predicates acted on all of currently selected nodes,
similar to how multiple predicates works...

Thanks all who responded for helping me clear that up...
Josh



On Wed, 11 Aug 2004 12:53:24 -0600, M. David Peterson
<m(_dot_)david(_at_)mdptws(_dot_)com> wrote:
Either way they are evaluated they are going to return the same node set (in
this particular case at least)..  take out the "in" attribute from the first
"two" child element and see if the results are as you would expect...

<M:D/>



Josh Canfield wrote:

When I was looking at this question I stumbled onto something that is
confusing me, I'm hoping someone here can enlighten me.

Given this xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<one>
  <two in="1">2.1</two>
  <two in="1">2.2</two>
  <two in="1">2.3</two>
</one>
<one>
  <two in="2">2.1</two>
  <two in="2">2.2</two>
  <two in="2">2.3</two>
</one>
<one>
  <two in="3">2.1</two>
  <two in="3">2.2</two>
  <two in="3">2.3</two>
</one>
</root>

If I want to copy all of the "two" nodes with an "in" attribute I could do 
this:
<xsl:copy-of select="/root/one/two[(_at_)in]"/>

But if I want to get the first "two" node with an "in" attribute, why
can't I do this?:
<xsl:copy-of select="/root/one/two[(_at_)in][1]"/>

As I read the XPath spec, it seems to be saying that the second
predicate should be evaluated using the context list and proximity
position of the node set generated by the completion of the first
predicate. Both XALAN and Saxon seem to be treating the previous as
equivalent to this:
<xsl:copy-of select="/root/one/two[(_at_)in and position()=1]"/>

Am I reading this incorrectly?

http://www.w3.org/TR/xpath#section-Location-Steps
"The initial node-set is filtered by the first predicate to generate a
new node-set; this new node-set is then filtered using the second
predicate, and so on."
http://www.w3.org/TR/xpath#predicates
"The proximity position of a member of a node-set with respect to an
axis is defined to be the position of the node in the node-set ordered
in document order if the axis is a forward axis and ordered in reverse
document order if the axis is a reverse axis. The first position is
1."

Thanks,
Josh



<Prev in Thread] Current Thread [Next in Thread>