473,322 Members | 1,523 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,322 software developers and data experts.

mozilla and XPath Expressions


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

Jul 20 '05 #1
2 6037


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/

Jul 20 '05 #2
Martin Honnen <ma*******@yahoo.de> wrote in message news:<3f******@olaf.komtel.net>...
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
Jul 20 '05 #3

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

Similar topics

3
by: Kevin | last post by:
I know this has probably been discussed many times before (I found answers when I searched yesterday), but I still can't get it to work... I have an attribute @OID that can contain any...
7
by: Sebastian Petzelberger | last post by:
Hi group, please give me an example of a xpath with regex or better a link with examples. Thanks in advance, Sebastian
3
by: gimme_this_gimme_that | last post by:
I once downloaded a shareware program that allowed you to open an xml file, click on a text or an attribute, an then see the xpath expression that would fetch that data. The program didn't...
1
by: Arndt Jonasson | last post by:
The definition of XPath 1.0 says "A QName in the node test is expanded into an expanded-name using the namespace declarations from the expression context. This is the same way expansion is done...
4
by: syed.akhlaq | last post by:
Hi, Does anyone know how can I validate XPath expressions using xsd schema? Thanks
4
by: Weston | last post by:
Are there any quick, reliable shortcuts to determine if two arbitrary XPath expressions yield result sets that intersect? The obviously way would be to iterate over the set of nodes from one...
1
by: reyesvsn | last post by:
Hello everybody, I have a question concerning XPath expressions and namespaces. Consider this XML: <?xml version="1.0" encoding="UTF-8"?> <newsMessage ...
4
by: ducttape | last post by:
Hi, I have been trying for several days to read XML files into my program. I have been following several good tutorials on the internet, but I am struggling because the particular XML files that I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.