Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
How to use the contextNode in document.evaluate()?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Björn Langhof  
View profile  
 More options Sep 29 2006, 11:54 am
Newsgroups: comp.lang.javascript
From: Björn Langhof <im...@web.de>
Date: Fri, 29 Sep 2006 17:54:36 +0200
Local: Fri, Sep 29 2006 11:54 am
Subject: How to use the contextNode in document.evaluate()?
Hello.

I want to evaluate a XPath-Expression only on a subtree of the whole
xml-document.

1. I select a node of the XML-document
2. Then a want to select specific nodes below the node chosen in 1.

I thought the parameter contextNode in
var xpathResult = document.evaluate(xpathExpression, contextNode,
namespaceResolver, resultType, result)
would do this like described here
http://www-xray.ast.cam.ac.uk/~jgraham/mozilla/xpath-tutorial.html

But this doesn't work. It still selects all nodes from the whole
document and not only of the subtree.

At the bottom is a short example.

The XML-Document:
<gods>
        <god name="Kibo">
                <amount value="3"/>
        </god>
        <god name="Xibo">
                <amount value="6"/>
        </god>
<\/gods>

1. I select the node <god name="Kibo"> and name it godKibo.
2. I want to get the nodelist of all <amount>-Nodes below godKibo.
var expr = '//amount';
var xpathResult = xmlDocument.evaluate(
        expr,
        godKibo,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null
);

But this gives a list of all <amount>-Nodes of the document. How do I
have to change expr to work?

I know if I set:

var expr = '//god[@name="Kibo"]/amount';
var xpathResult = xmlDocument.evaluate(
        expr,
        xmlDocument,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null
);

it will work. But I don't want to repeat the expression to find the
godKibo-node.

Thanks for your help.

Greetings,
   Björn

Here is the full example.

var xmlSource = '';
xmlSource += '<gods>';
xmlSource += '<god name="Kibo">';
xmlSource += '<amount value="3"/>';
xmlSource += '</god>';
xmlSource += '<god name="Xibo">';
xmlSource += '<amount value="6"/>';
xmlSource += '</god>';
xmlSource += '<\/gods>';
var xmlDocument = new DOMParser().parseFromString(xmlSource, 'text/xml');

var xmlSerializer = new XMLSerializer();

// Selecting the node god with the name Kibo

var godKibo = xmlDocument.evaluate(
        '//god[@name="Kibo"]',
        xmlDocument,
        null,
        XPathResult.FIRST_ORDERED_NODE_TYPE,
        null
).singleNodeValue;

// Selecting the node amount below the node godKibo

var expr = '//amount';

var xpathResult = xmlDocument.evaluate(
        expr,
        godKibo,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null
);

// This alert returns 2 although I thought it would
// search only below the godKibo node and return 1

alert(xpathResult.snapshotLength);


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Honnen  
View profile  
 More options Sep 29 2006, 12:01 pm
Newsgroups: comp.lang.javascript
From: Martin Honnen <mahotr...@yahoo.de>
Date: Fri, 29 Sep 2006 18:01:37 +0200
Local: Fri, Sep 29 2006 12:01 pm
Subject: Re: How to use the contextNode in document.evaluate()?

Björn Langhof wrote:
> 1. I select the node <god name="Kibo"> and name it godKibo.
> 2. I want to get the nodelist of all <amount>-Nodes below godKibo.
> var expr = '//amount';

Use a relative XPath expression then, if you use
  //amount
then you always search from the root node, if you simply used e.g
   amount
then child elements of name 'amount' relative to the context nodes are
searched. If you use e.g.
  .//amount
then descendant elements of name 'amount' relative to the context nodes
are searched.

--

        Martin Honnen
        http://JavaScript.FAQTs.com/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Björn Langhof  
View profile  
 More options Sep 29 2006, 12:22 pm
Newsgroups: comp.lang.javascript
From: Björn Langhof <im...@web.de>
Date: Fri, 29 Sep 2006 18:22:08 +0200
Local: Fri, Sep 29 2006 12:22 pm
Subject: Re: How to use the contextNode in document.evaluate()?
Martin Honnen schrieb:

> Björn Langhof wrote:

>> 1. I select the node <god name="Kibo"> and name it godKibo.
>> 2. I want to get the nodelist of all <amount>-Nodes below godKibo.
>> var expr = '//amount';

> Use a relative XPath expression then, if you use
>  //amount
> then you always search from the root node, if you simply used e.g
>   amount
> then child elements of name 'amount' relative to the context nodes are
> searched. If you use e.g.
>  .//amount
> then descendant elements of name 'amount' relative to the context nodes
> are searched.

Works great. Thank you very much for the quick response.

Björn


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »