Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > mozilla and XPath Expressions

Reply
Thread Tools

mozilla and XPath Expressions

 
 
Neil Zanella
Guest
Posts: n/a
 
      12-09-2003

Hello,

I would like to know whether the mozilla web browser has built in support
for searching XML documents via XPath expressions as with IE's xmlobject's
and xmlDoc's function selectNodes() or similar.

Thanks!

Neil

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      12-09-2003


Neil Zanella wrote:

> I would like to know whether the mozilla web browser has built in support
> for searching XML documents via XPath expressions as with IE's xmlobject's
> and xmlDoc's function selectNodes() or similar.


Mozilla has XPath support but not the restricted MSXML selectNodes but
rather a more complete approach that is being under development, Mozilla
attempts to implement the W3C DOM Level 3 XPath module:
http://www.w3.org/TR/2003/CR-DOM-Lev...Path-20030331/

At the heart of that is method
document.evaluate(
string xpathexpression,
node contextNode,
XPathNSResolver namespaceresolver,
number desiredResultType,
object resultObject
)

Some examples on how to use it, lets create a short example document first:

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

var xmlSerializer = new XMLSerializer();

Selecting a single node:

var xpathResult = xmlDocument.evaluate(
'//god',
xmlDocument,
null,
9,
null
);

if (xpathResult.singleNodeValue) {
alert(xmlSerializer.serializeToString(xpathResult. singleNodeValue));
}


Selecting an ordered node list:

var xpathResult = xmlDocument.evaluate(
'//god',
xmlDocument,
null,
5,
null
);

var resultNode;

while ((resultNode = xpathResult.iterateNext())) {
alert(xmlSerializer.serializeToString(resultNode)) ;
}


Evaluating an expression that returns a number:

var xpathResult = xmlDocument.evaluate(
'count(//*/@*)',
xmlDocument,
null,
0,
null
);

if (xpathResult.resultType == 1) {
alert('Counted ' + xpathResult.numberValue + ' nodes.');
}


That should help you to get started if you then have a look at the
specification.
Or ask here if you have further questions.
There is even some attempt to extend XPath to HTML documents:

<html>
<head>
<title>"XPath" on HTML in Mozilla</title>
<script type="text/javascript">
function countCenteredParagraphs () {
if (typeof document.evaluate != 'undefined') {
var xpathResult =
document.evaluate(
'count(//p[@align = "center"])',
document,
null,
1,
null
);
alert('Found ' + xpathResult.numberValue + ' centered paragraphs.');
}
}
window.onload = countCenteredParagraphs;
</script>
</head>
<body>
<p align="center">
A paragraph with centered text.
</p>
<p align="center">
A second paragraph with centered text.
</p>
</body>
</html>
--

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

 
Reply With Quote
 
 
 
 
Neil Zanella
Guest
Posts: n/a
 
      12-14-2003
Martin Honnen <> wrote in message news:<>...
> Neil Zanella wrote:
>
> > I would like to know whether the mozilla web browser has built in support
> > for searching XML documents via XPath expressions as with IE's xmlobject's
> > and xmlDoc's function selectNodes() or similar.

>
> Mozilla has XPath support but not the restricted MSXML selectNodes but
> rather a more complete approach that is being under development, Mozilla
> attempts to implement the W3C DOM Level 3 XPath module:
> http://www.w3.org/TR/2003/CR-DOM-Lev...Path-20030331/


Thank you for your reply. I recently upgraded to mozilla 1.4.1 and I can see
that it has a "DOM Inspector" feature accessible from the menu bar under
Tools -> Web Development. I guess this is what you must be referring to.
So how do I run the code snippets that you posted?

> At the heart of that is method
> document.evaluate(
> string xpathexpression,
> node contextNode,
> XPathNSResolver namespaceresolver,
> number desiredResultType,
> object resultObject
> )
>
> Some examples on how to use it, lets create a short example document first:
>
> var xmlSource = '';
> xmlSource += '<gods>';
> xmlSource += '<god name="Kibo" />';
> xmlSource += '<god name="Xibo" />';
> xmlSource += '<\/gods>';
> var xmlDocument = new DOMParser().parseFromString(xmlSource, 'text/xml');


> That should help you to get started if you then have a look at the
> specification. Or ask here if you have further questions.
> There is even some attempt to extend XPath to HTML documents:


Well, I thought that at least this should already be possible as far
as XHTML is concerned since XHTML is a reformulation of HTML as an XML
application, right?

Thanks!

Neil
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to dynamic XPath expressions in databinding to XmlDataSource? Thor W Hammer ASP .Net 0 03-14-2006 09:08 AM
XPath: efficiency in xpath expressions Tjerk Wolterink XML 1 11-13-2004 06:03 PM
regex xpath expressions Timo Nentwig XML 4 02-02-2004 12:04 PM
Use Parameter value in match or select - 'Parameterized XPath expressions ?' Colin Toal XML 3 01-23-2004 12:39 AM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57