471,338 Members | 993 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Strange problem with count

Hello,

My last few posts have been revolving aroung the same problem, and I
still cant solve it and I would be really appreciate if anyone could
spot a problem.

a section of my XML goes like

.....
<parent>
<child>
<grandchild1>
<grandchild2>
<child>
<parent>

/...

I need to describe a numeric link between grandchild elements and the
child elements.

I have been using count(preceding::*) to id the cells numerically:
<xsl:template select="grandchild1|grandchild2">
<MyID>
Child: <xsl:value-of select="count(../preceding::*)"/>
Grandchild: <xsl:value-of select="count(./preceding::*)"/>
</MyID>
</xsl:template>
However, the first grandchild1 element that runs this has the same
count as the child element. (35 and 35). The second grandchild element
that runs is incremented by one as expected, 35 and 36, meaning it
seems 1 too low. The obvious problem is the first counts are returned
the same, negating the idea of unique numeric ID's.

Are these results to be expected? I do not know why/how the ../ and ./
return the same value (surely the ../preceding has come back a node in
that first case and therefore should be -1 of the grandchild1?)

I know a simple fix is just to add one onto all of the grandchilds
(count(./preceding::*)+1) but it seems slight dodgey, i.e. a fix to a
potential problem in my XSL.

I would really appreciate is anyone could help me understand why this
does not work, so that I can fix it properly.

Any help will again be most appreciated.

Thanks, Rob.

Aug 2 '05 #1
3 1510

<ro************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello,

My last few posts have been revolving aroung the same problem, and I
still cant solve it and I would be really appreciate if anyone could
spot a problem.

a section of my XML goes like

....
<parent>
<child>
<grandchild1>
<grandchild2>
<child>
<parent>

/...

I need to describe a numeric link between grandchild elements and the
child elements.

I have been using count(preceding::*) to id the cells numerically:
It is not possible to identify a node uniquely using only one axis.


<xsl:template select="grandchild1|grandchild2">
<MyID>
Child: <xsl:value-of select="count(../preceding::*)"/>
Grandchild: <xsl:value-of select="count(./preceding::*)"/>
</MyID>
</xsl:template>
However, the first grandchild1 element that runs this has the same
count as the child element. (35 and 35). The second grandchild element
that runs is incremented by one as expected, 35 and 36, meaning it
seems 1 too low. The obvious problem is the first counts are returned
the same, negating the idea of unique numeric ID's.

Are these results to be expected?
Yes.

The "preceding" and "ancestor" axis are non-overlapping and can be thought
of as the X and Y axis in a two-dimensional space (plane).

To quote the XPath 1.0 spec:

"the preceding axis contains all nodes in the same document as the context
node that are before the context node in document order, excluding any
ancestors and excluding attribute nodes and namespace nodes"

http://www.w3.org/TR/xpath#axes

I do not know why/how the ../ and ./
return the same value (surely the ../preceding has come back a node in
that first case and therefore should be -1 of the grandchild1?)

I know a simple fix is just to add one onto all of the grandchilds
(count(./preceding::*)+1) but it seems slight dodgey, i.e. a fix to a
potential problem in my XSL.

I would really appreciate is anyone could help me understand why this
does not work, so that I can fix it properly.
An unique ID can be constructed taking both the "ancestor" and "preceding"
axis into account and generating a *pair* of numbers.

Another way is to use xsl:number
Cheers,
Dimitre Novatchev

Any help will again be most appreciated.

Thanks, Rob.

Aug 2 '05 #2
Another quote from the XPath 1.0 Spec, which describes this topic even more
precisely:

"NOTE: The ancestor, descendant, following, preceding and self axes
partition a document (ignoring attribute and namespace nodes): they do not
overlap and together they contain all the nodes in the document."
Cheers,
Dimitre Novatchev
"Dimitre Novatchev" <x@yyy.com> wrote in message
news:42***********************@authen.white.readfr eenews.net...

<ro************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello,

My last few posts have been revolving aroung the same problem, and I
still cant solve it and I would be really appreciate if anyone could
spot a problem.

a section of my XML goes like

....
<parent>
<child>
<grandchild1>
<grandchild2>
<child>
<parent>

/...

I need to describe a numeric link between grandchild elements and the
child elements.

I have been using count(preceding::*) to id the cells numerically:


It is not possible to identify a node uniquely using only one axis.


<xsl:template select="grandchild1|grandchild2">
<MyID>
Child: <xsl:value-of select="count(../preceding::*)"/>
Grandchild: <xsl:value-of select="count(./preceding::*)"/>
</MyID>
</xsl:template>
However, the first grandchild1 element that runs this has the same
count as the child element. (35 and 35). The second grandchild element
that runs is incremented by one as expected, 35 and 36, meaning it
seems 1 too low. The obvious problem is the first counts are returned
the same, negating the idea of unique numeric ID's.

Are these results to be expected?


Yes.

The "preceding" and "ancestor" axis are non-overlapping and can be thought
of as the X and Y axis in a two-dimensional space (plane).

To quote the XPath 1.0 spec:

"the preceding axis contains all nodes in the same document as the context
node that are before the context node in document order, excluding any
ancestors and excluding attribute nodes and namespace nodes"

http://www.w3.org/TR/xpath#axes

I do not know why/how the ../ and ./
return the same value (surely the ../preceding has come back a node in
that first case and therefore should be -1 of the grandchild1?)

I know a simple fix is just to add one onto all of the grandchilds
(count(./preceding::*)+1) but it seems slight dodgey, i.e. a fix to a
potential problem in my XSL.

I would really appreciate is anyone could help me understand why this
does not work, so that I can fix it properly.


An unique ID can be constructed taking both the "ancestor" and "preceding"
axis into account and generating a *pair* of numbers.

Another way is to use xsl:number
Cheers,
Dimitre Novatchev

Any help will again be most appreciated.

Thanks, Rob.


Aug 2 '05 #3
Ah, that makes real sense. Thanks! That last quote was a good one.

Thanks again, Rob

Dimitre Novatchev wrote:
Another quote from the XPath 1.0 Spec, which describes this topic even more
precisely:

"NOTE: The ancestor, descendant, following, preceding and self axes
partition a document (ignoring attribute and namespace nodes): they do not
overlap and together they contain all the nodes in the document."
Cheers,
Dimitre Novatchev
"Dimitre Novatchev" <x@yyy.com> wrote in message
news:42***********************@authen.white.readfr eenews.net...

<ro************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello,

My last few posts have been revolving aroung the same problem, and I
still cant solve it and I would be really appreciate if anyone could
spot a problem.

a section of my XML goes like

....
<parent>
<child>
<grandchild1>
<grandchild2>
<child>
<parent>

/...

I need to describe a numeric link between grandchild elements and the
child elements.

I have been using count(preceding::*) to id the cells numerically:


It is not possible to identify a node uniquely using only one axis.


<xsl:template select="grandchild1|grandchild2">
<MyID>
Child: <xsl:value-of select="count(../preceding::*)"/>
Grandchild: <xsl:value-of select="count(./preceding::*)"/>
</MyID>
</xsl:template>
However, the first grandchild1 element that runs this has the same
count as the child element. (35 and 35). The second grandchild element
that runs is incremented by one as expected, 35 and 36, meaning it
seems 1 too low. The obvious problem is the first counts are returned
the same, negating the idea of unique numeric ID's.

Are these results to be expected?


Yes.

The "preceding" and "ancestor" axis are non-overlapping and can be thought
of as the X and Y axis in a two-dimensional space (plane).

To quote the XPath 1.0 spec:

"the preceding axis contains all nodes in the same document as the context
node that are before the context node in document order, excluding any
ancestors and excluding attribute nodes and namespace nodes"

http://www.w3.org/TR/xpath#axes

I do not know why/how the ../ and ./
return the same value (surely the ../preceding has come back a node in
that first case and therefore should be -1 of the grandchild1?)

I know a simple fix is just to add one onto all of the grandchilds
(count(./preceding::*)+1) but it seems slight dodgey, i.e. a fix to a
potential problem in my XSL.

I would really appreciate is anyone could help me understand why this
does not work, so that I can fix it properly.


An unique ID can be constructed taking both the "ancestor" and "preceding"
axis into account and generating a *pair* of numbers.

Another way is to use xsl:number
Cheers,
Dimitre Novatchev

Any help will again be most appreciated.

Thanks, Rob.



Aug 2 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by hall | last post: by
reply views Thread by Marc Robitaille | last post: by
10 posts views Thread by Henk Ernst Blok | last post: by
3 posts views Thread by Michael Meckelein | last post: by
10 posts views Thread by Bernard Liang | last post: by
10 posts views Thread by John Kraft | last post: by
1 post views Thread by Blackstar | last post: by
12 posts views Thread by Gilles Ganault | last post: by

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.