469,352 Members | 1,694 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Cannot find the p node with selectNodes

Hi All!

In the code below, I am reading in an xhtml document and attempting to use
selectNodes to find a <p id="rmb"> node..

But the result is:
2 - */*
0 - */p[@id = "rmb"]

Can anyone suggest what I am doing wrong?

Any ideas would be most appreciated!

Rob
:)
====
xmlTest.asp
====
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<%

var xmlDocument = Server.CreateObject("Msxml2.DOMDocument.3.0");
xmlDocument.async = false;
xmlDocument.setProperty ("ServerHTTPRequest", true);
xmlDocument.setProperty ("SelectionLanguage", "XPath");
var loaded = xmlDocument.load(Server.MapPath("basic.htm"));

if (loaded)
{
var editable = xmlDocument.selectNodes ("*/*");
Response.write (editable.length + " - " + editable.expr + "<br>");

var editable = xmlDocument.selectNodes ("*/p[@id = \"rmb\"]");
Response.write (editable.length + " - " + editable.expr + "<br>");
} // end if

%>

====
basic.htm
====
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Heading</h1>
<p>Body Text</p>
<p id="rmb">Body Text</p>
</body>
</html>
Jul 19 '05 #1
7 2525
Try:

//p[@id='rmb']

Chris.
"Robert Mark Bram" <none> wrote in message
news:40***********************@news.optusnet.com.a u...
Hi All!

In the code below, I am reading in an xhtml document and attempting to use
selectNodes to find a <p id="rmb"> node..

But the result is:
2 - */*
0 - */p[@id = "rmb"]

Can anyone suggest what I am doing wrong?

Any ideas would be most appreciated!

Rob
:)
====
xmlTest.asp
====
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<%

var xmlDocument = Server.CreateObject("Msxml2.DOMDocument.3.0");
xmlDocument.async = false;
xmlDocument.setProperty ("ServerHTTPRequest", true);
xmlDocument.setProperty ("SelectionLanguage", "XPath");
var loaded = xmlDocument.load(Server.MapPath("basic.htm"));

if (loaded)
{
var editable = xmlDocument.selectNodes ("*/*");
Response.write (editable.length + " - " + editable.expr + "<br>");

var editable = xmlDocument.selectNodes ("*/p[@id = \"rmb\"]");
Response.write (editable.length + " - " + editable.expr + "<br>");
} // end if

%>

====
basic.htm
====
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Heading</h1>
<p>Body Text</p>
<p id="rmb">Body Text</p>
</body>
</html>

Jul 19 '05 #2
Try
//*[name(.) = 'p' and @id = 'rmb'

Cheers

Pravin Pati
Microsoft(R) MVP
Jul 19 '05 #3
Hi Chris,

Thank you for the response.
//p[@id='rmb']


This did not work.. it gave me:
0 - //p[@id='rmb']

I am at a loss to explain why. :-\

Rob
:)
Jul 19 '05 #4
Hi Pravin,

Thank you very much - this worked!
//*[name(.) = 'p' and @id = 'rmb']


The result I obtained:
0 - //p[@id='rmb']
1 - //*[name(.) = 'p' and @id = 'rmb']

Do you happend to know why the first one (Chris Barber's suggestion) deosn't
work for me?

Thank you again!

Rob
:)
Jul 19 '05 #5
Post a real (eg. XML validating XHTML document that shows this issue so I
can run it in Xselerator). There is no reason why the Xpath I posted should
not work unless your XHTML is a bit funky (eg. namespaces etc., DOM type, or
the method of executing the XPath statement).

This XML:

<?xml version="1.0"?>
<root>
<p id="rmb"/>
</root>

And this XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//p[@id='rmb']">
<p>Found the node</p>
</xsl:template>
</xsl:stylesheet>

Produce:

<?xml version="1.0" encoding="UTF-16"?>
<p>Found the node</p>

Proving that the XPath works (at least in XSLT implementation which should
be identical to selectSingleNode implementation.

As a test, the following html client side Javascript gave an alert box
showing the selected node XML indicating that the node was found.

<html>
<head>
</head>
<body>

<script language="Javascript">

var pobjXML = new ActiveXObject("Msxml2.DOMDocument.4.0");
pobjXML.async = false;
pobjXML.load("mydoc.xml");
alert("XML document is: " + pobjXML.xml);
var pstrXPath = "//p[@id='rmb']";
alert("XPath is: " + pstrXPath);
var pobjNode = pobjXML.selectSingleNode(pstrXPath);
if (pobjNode){
alert("Selected node xml is: " + pobjNode.xml);
}
else{
alert('Node not found');
}

</script>
<p>Test completed</p>
</body>
</html>

Chris.

"Chris Barber" <ch***@blue-canoe.co.uk.NOSPAM> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Try:

//p[@id='rmb']

Chris.
"Robert Mark Bram" <none> wrote in message
news:40***********************@news.optusnet.com.a u...
Hi All!

In the code below, I am reading in an xhtml document and attempting to use
selectNodes to find a <p id="rmb"> node..

But the result is:
2 - */*
0 - */p[@id = "rmb"]

Can anyone suggest what I am doing wrong?

Any ideas would be most appreciated!

Rob
:)
====
xmlTest.asp
====
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<%

var xmlDocument = Server.CreateObject("Msxml2.DOMDocument.3.0");
xmlDocument.async = false;
xmlDocument.setProperty ("ServerHTTPRequest", true);
xmlDocument.setProperty ("SelectionLanguage", "XPath");
var loaded = xmlDocument.load(Server.MapPath("basic.htm"));

if (loaded)
{
var editable = xmlDocument.selectNodes ("*/*");
Response.write (editable.length + " - " + editable.expr + "<br>");

var editable = xmlDocument.selectNodes ("*/p[@id = \"rmb\"]");
Response.write (editable.length + " - " + editable.expr + "<br>");
} // end if

%>

====
basic.htm
====
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Heading</h1>
<p>Body Text</p>
<p id="rmb">Body Text</p>
</body>
</html>


Jul 19 '05 #6
Hi Chis!
Post a real (eg. XML validating XHTML document that shows this issue so I
can run it in Xselerator).


Maybe I misunderstand you, but basic.htm is valid and correct XML -
Dreamweaver and XML Spy both validate the document..

Rob
:)

====
basic.htm
====
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Heading</h1>
<p>Body Text</p>
<p id="rmb">Body Text</p>
</body>
</html>
Jul 19 '05 #7
Your namespace on html element will be preventing my XPath from operating in
an XMLDOM environment.

Have a read about 'default namespaces' when using XPath statements to select
nodes.

I think that the XPath Pravin provided may be namespace insensitive?

Chris.

"Robert Mark Bram" <none> wrote in message
news:40**********************@news.optusnet.com.au ...
Hi Chis!
Post a real (eg. XML validating XHTML document that shows this issue so I
can run it in Xselerator).


Maybe I misunderstand you, but basic.htm is valid and correct XML -
Dreamweaver and XML Spy both validate the document..

Rob
:)

====
basic.htm
====
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Heading</h1>
<p>Body Text</p>
<p id="rmb">Body Text</p>
</body>
</html>

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by chris yoker via DotNetMonster.com | last post: by
3 posts views Thread by chris yoker via DotNetMonster.com | last post: by
3 posts views Thread by Sharon | last post: by
16 posts views Thread by TT (Tom Tempelaere) | last post: by
4 posts views Thread by =?Utf-8?B?VG9yZW4gVmFsb25l?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.