473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

position() in XPath has wrong reference

Hi,

my XML-Document hat this structure
<book>
<section>
<para />
<para />
</section>
<section>
<para />
<para />
</section>
</book>...

Now I need to find the third para-element of all para-Elements.
In my XSLT-Stylesheet I write

<xsl:template match="para">
<xsl:if test="position()= 3" >
do something
</xsl:if>
</xsl:template>

This does not work. He finds nothing. When I test
position()=1
he finds the first para of the first section and the first para of the
second section

Is there any possibility to find out the position of the current para-node
comparing to _all_ para-elements within the whole document?

Thanks

Matthias
ml@mlohrer.de


Nov 11 '05 #1
3 2026
Hi,

what you say is right, but it doesn't answer my question:

when I use
<xsl:template match="//section/para"> ...

it is the same:
<xsl:if test="position()= 3" > will have no result
and <xsl:if test="position()= 1" > has 2 results

when I use
<xsl:template match="//para">
<xsl:if test="position()= 1" >
I get 2 results

when I use
<xsl:template match="//para">
<xsl:if test="position()= 3" >
I get 0 results.

So the logic is always the same:
The context is made up by the para-Element with the same
parent-section-Element.

I seems not to be possible to create a context that consists only of
all para-Elements without respect of the parent-section-element, and this
is, what I'm looking for.

Matthias
"Yuriy Solodkyy" <sw@softasap.com> schrieb im Newsbeitrag
news:Oo**************@TK2MSFTNGP10.phx.gbl...
Hi,
See the "position Function" reference in "Microsoft XML Core Services
(MSXML) 4.0 - XPath Reference". The position() function is sensetive to the calling context. For exaple, if you apply-templates to select "//para" you will have different result, or even if select is section/para than if you
apply templates separately for each section or default templates are used.

The sample SDK describes this in details.

Yuriy

"Matthias Lohrer" <ma*************@mlohrer.de> wrote in message
news:3f*********************@read.news.de.uu.net.. .
Hi,

my XML-Document hat this structure
<book>
<section>
<para />
<para />
</section>
<section>
<para />
<para />
</section>
</book>...

Now I need to find the third para-element of all para-Elements.
In my XSLT-Stylesheet I write

<xsl:template match="para">
<xsl:if test="position()= 3" >
do something
</xsl:if>
</xsl:template>

This does not work. He finds nothing. When I test
position()=1
he finds the first para of the first section and the first para of the
second section

Is there any possibility to find out the position of the current para-node comparing to _all_ para-elements within the whole document?

Thanks

Matthias
ml@mlohrer.de


Nov 11 '05 #2
The following sample returns one element, see:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:output method="xml" indent="yes" encoding="utf-8"/>

<xsl:template match="/">
<r>
<xsl:apply-templates select="//para" />
</r>
</xsl:template>

<xsl:template match="para">
<xsl:if test="position() = 3">
<yes />
</xsl:if>
</xsl:template>

</xsl:stylesheet>

"Matthias Lohrer" <ma*************@mlohrer.de> wrote in message
news:3f*********************@read.news.de.uu.net.. . Hi,

what you say is right, but it doesn't answer my question:

when I use
<xsl:template match="//section/para"> ...

it is the same:
<xsl:if test="position()= 3" > will have no result
and <xsl:if test="position()= 1" > has 2 results

when I use
<xsl:template match="//para">
<xsl:if test="position()= 1" >
I get 2 results

when I use
<xsl:template match="//para">
<xsl:if test="position()= 3" >
I get 0 results.

So the logic is always the same:
The context is made up by the para-Element with the same
parent-section-Element.

I seems not to be possible to create a context that consists only of
all para-Elements without respect of the parent-section-element, and this
is, what I'm looking for.

Matthias
"Yuriy Solodkyy" <sw@softasap.com> schrieb im Newsbeitrag
news:Oo**************@TK2MSFTNGP10.phx.gbl...
Hi,
See the "position Function" reference in "Microsoft XML Core Services
(MSXML) 4.0 - XPath Reference". The position() function is sensetive to

the
calling context. For exaple, if you apply-templates to select "//para"

you
will have different result, or even if select is section/para than if you
apply templates separately for each section or default templates are used.
The sample SDK describes this in details.

Yuriy

"Matthias Lohrer" <ma*************@mlohrer.de> wrote in message
news:3f*********************@read.news.de.uu.net.. .
Hi,

my XML-Document hat this structure
<book>
<section>
<para />
<para />
</section>
<section>
<para />
<para />
</section>
</book>...

Now I need to find the third para-element of all para-Elements.
In my XSLT-Stylesheet I write

<xsl:template match="para">
<xsl:if test="position()= 3" >
do something
</xsl:if>
</xsl:template>

This does not work. He finds nothing. When I test
position()=1
he finds the first para of the first section and the first para of the
second section

Is there any possibility to find out the position of the current

para-node comparing to _all_ para-elements within the whole document?

Thanks

Matthias
ml@mlohrer.de



Nov 11 '05 #3
Indeed, now it works.
I see the difference: You created a new context by using
<xsl:apply-templates select="//para"> instead of modifying the
match-attribute of the template. We in germany say "Gewusst wie!"
Thank you!

"Yuriy Solodkyy" <sw@softasap.com> schrieb im Newsbeitrag
news:uR**************@TK2MSFTNGP12.phx.gbl...
The following sample returns one element, see:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

<xsl:output method="xml" indent="yes" encoding="utf-8"/>

<xsl:template match="/">
<r>
<xsl:apply-templates select="//para" />
</r>
</xsl:template>

<xsl:template match="para">
<xsl:if test="position() = 3">
<yes />
</xsl:if>
</xsl:template>

</xsl:stylesheet>

"Matthias Lohrer" <ma*************@mlohrer.de> wrote in message
news:3f*********************@read.news.de.uu.net.. .


Nov 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joshua Beall | last post by:
Hi All, I have a task that should be very simple but I'm running into trouble. All I want to do is query a document using XPath, and save the resulting XML in a string. Here's that I am trying...
2
by: Hans-Michael Rupp | last post by:
Hallo! How can find out the postion of a node with a particular content in the nodeset WITHOUT being in the context? I would like have something like <xsl:variable name="position"...
1
by: Felix Natter | last post by:
hi, I have a structure like: <topic name="xxx"> <subtopic name="subxxx"> <subsubtopic name="subsubxxx"> </subsubtopic> </subtopic> </topic>
2
by: Mark Bordner | last post by:
Hi All, Given the following XML, I've been trying to figure out what XPATH expression will give me the "position" of a <lineitem> within the <shipto> that is its grandparent. To give you an...
1
by: Hollywood | last post by:
I have the following XSD created in VS.NET 2003: <?xml version="1.0" encoding="utf-8" ?> <xs:schema id="ReferralSchama" targetNamespace="http://test.org/Referral"...
1
by: awebguynow | last post by:
After reviewing posts, I believe that it is impossible, to get, in 1 select statement the position of a xpath with qualifying predicate. OK, that sounded like jibberish - let me try again. ...
3
by: awebguynow | last post by:
with an xpath expr: step1/step2 I'm trying to get the position() of step2, using the qualifying predicate. I've seen some suggestions, of ways to do this, using count() instead, but I'm not...
5
by: Christof Nordiek | last post by:
When I load a XML-Document from a file, and then get an XmlNode from it, is there any way to get the its original position in the file (line, character)? I want to report it, when there is...
1
by: SiJP | last post by:
Consider the following XML <?xml version="1.0" encoding="UTF-8"?> <Root> <Data> <ID>1005</ID> <Reference> <Reference>ABC</Reference> <Country>UK</Country> </Reference>
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.