db*****@gmail.com wrote:
This is what I want to do:
<html>
<xml id="myXml">
<book title="book1" />
<book title="book2" />
</xml>
<div id="out"><!-- I would output the xml formatted nicely for the
user in this div</div>
<script type="text/javascript">
var xmlDoc = document.getElementById("myXml");
alert(xmlDoc.hasChildNodes);
</script>
</html>
But that doesn't work, it says document.getElementById("xmlXml") has
no properties. How do I do a scenario like this?
The first thing you would need to make sure is Valid markup. The above is
not. document.getElementById() where `document' refers to an object
implementing the HTMLDocument interface finds *HTML* elements with
attributes of type ID; `xml' is not an HTML element.
You might have better luck with document.getElementsByTagName("xml")[0] and
continuing from there. But that does not make your markup Valid; to embed
XML in HTML, you have to use XHTML and namespaces, for example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" [
<!ENTITY myxml "http://foo.example/bar">
]>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:myxml="&myxml;">
<head>
<title>Embedded XML Data</title>
</head>
<body>
<myxml:xml>
<myxml:book title="book1" />
<myxml:book title="book1" />
</myxml:xml>
<script type="text/javascript">
// works only with application/xhtml+xml
var xmlDoc = document.getElementsByTagNameNS("&myxml;", "xml");
// "[object NodeList]" in Gecko
window.alert(xmlDoc[0].childNodes);
</script>
</body>
</html>
That said, you should solve this with an external XML resource and XSLT,
preferably server-side, instead.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16