Featured post
javascript - How do I retrieve the value of a specific XML node by path? -
i looking javascript solution problem:
i have xml document loaded way of typical ajax method:
var xml = http.responsexml;
consider following example xml snippet:
<main> <primary> <enabled>true</enabled> </primary> <secondary> <enabled>true</enabled> </secondary> </main>
i value of node specifying path follows:
var second_enabled = getnodevaluebypath('main/secondary/enabled', xml);
i cannot find concise way of doing this. seem forced iterate through node collections after using getelementsbytagname
, like.
how construct method getnodevaluebypath
or there construct in javascript allows this?
i not versed in javascript.
edit: here example shows attempt @ using xpath , how failing:
xml:
<?xml version="1.0" ?> <main xmlns="example.com"> <primary> <enabled>true</enabled> </primary> <secondary> <enabled>false</enabled> </secondary> </main>
javascript: (these relevant functions only)
function usehttpresponse() { if (http.readystate == 4) { if(http.status == 200) { var xml = http.responsexml; var evalue = getxmlvaluebypath('/main/secondary/enabled', xml); alert(evalue); } } } function getxmlvaluebypath(nodepath, xml) { var result = xml.evaluate(nodepath, xml, null, xpathresult.string_type, null).stringvalue; return result; }
i using above javascript without additional libraries. using mozilla firefox 3.6.13 uses document.evaluate method select nodes (according this information w3schools). this application internal use , not have work on multiple browsers.
given these examples, alert dialog appear, contain no text. if remove string xmlns="example.com"
xml document, alert dialog appears text "false" desired.
debugging using firebug shows resultnode
empty string long namespace declared.
this faq: in xpath 1.0, qname test without prefix selects elements under null (or empty) namespace uri. need register prefix default namespace uri input source (example.com
).
how do dom 3 xpath api?
from http://www.w3.org/tr/dom-level-3-xpath/xpath.html#xpathevaluator-evaluate
evaluate
evaluates xpath expression string , returns result of specified type if possible.
parameters
[...]
resolver
of typexpathnsresolver
resolver permits translation of prefixes, includingxml
namespace prefix, within xpath expression appropriate namespace uris. if specifiednull
, namespace prefix within expression result indomexception
being thrown codenamespace_err
.
and https://developer.mozilla.org/en/dom/document.evaluate
var xpathresult = document.evaluate( xpathexpression, contextnode, namespaceresolver, resulttype, result);
namespaceresolver
function passed namespace prefixes , should return string representing namespace uri associated prefix. used resolve prefixes within xpath itself, can matched document.null
common html documents or when no namespace prefixes used.
following guide, can use document.creatensresolver
or function like:
function nsresolver(prefix) { return prefix == 'ex' ? 'example.com' : null; } function usehttpresponse() { if (http.readystate == 4) { if (http.status == 200) { var xml = http.responsexml; var evalue = xml.evaluate('string(/ex:main/ex:secondary/ex:enabled)', xml, nsresolver, xpathresult.string_type, null); alert(evalue.stringvalue); } } }
- Get link
- X
- Other Apps
Comments
Post a Comment