XPath in Firefox 2.0: accessing attribute value

Here should go questions about transforming XML with XSLT and FOP.
UncleRic
Posts: 4
Joined: Fri Oct 27, 2006 11:09 pm

XPath in Firefox 2.0: accessing attribute value

Post by UncleRic »

XPath neophyte here...

I want to be able to drill down into a DOM object within Firefox and extract a particular element's attributes and their values.

The Javascript code starts out as follows:

Code: Select all


// Selecting 'id' attribute of element 'project':
var xpathResult = document.evaluate("//project[@id]", domDoc, null, XPathResult.ANY_TYPE, null );
All I know for now, is how to grab the particular attribute. But I haven't found any info on how to extract its value(s). Is there a [@id].getValue structure or equiv?
fcsonline
Posts: 3
Joined: Wed Nov 08, 2006 4:44 pm

Post by fcsonline »

May be:

var xpathResult = document.evaluate("//project/@id", domDoc, null, XPathResult.ANY_TYPE, null );
fcsonline
Posts: 3
Joined: Wed Nov 08, 2006 4:44 pm

Post by fcsonline »

And put XPathResult.STRING_TYPE.

Look at this url

http://developer.mozilla.org/en/docs/In ... E_Constant
UncleRic
Posts: 4
Joined: Fri Oct 27, 2006 11:09 pm

Post by UncleRic »

Thanks for the reply.
Essentially I need to get a set of attributes with their name and values.

I found the following code at a Mozilla forum that takes an element (node) and return the attribute values (w/out the associated attribute names):

Code: Select all


// Firefox routine.
// Evaluate an XPath expression aExpression against a given DOM node
// or Document object (aNode), returning the results as an array
// thanks wanderingstan at morethanwarm dot mail dot com for the
// initial work.
function evaluateXPath(aNode, aExpr) {
var xpe = new XPathEvaluator();
var nsResolver = xpe.createNSResolver(aNode.ownerDocument == null ?
aNode.documentElement : aNode.ownerDocument.documentElement);
var result = xpe.evaluate(aExpr, aNode, nsResolver, 0, null);
var found = [];
var res;
while (res = result.iterateNext())
found.push(res);
return found;
}
The above routine is the closest so far to what I'm after; just missing the associated attribute names.

I tried the following...
PostPosted: Wed Nov 08, 2006 11:08 pm Post subject:
May be:

var xpathResult = document.evaluate("//project/@id", domDoc, null, XPathResult.ANY_TYPE, null );
And put XPathResult.STRING_TYPE.
I tried this approach and got the numeral '2'.

I'm sure there must be way to easily get the node name.

I'll revisit the Mozilla forum again and further explore the above code. I'm seeking the definition of XPathEvaluator. Docs are few and far between. Ditto with W3C docs.

Again, thanks for the info.
Any more ideas, suggestions are appreciated.
rawbot69
Posts: 6
Joined: Thu Jan 18, 2007 10:11 pm

Post by rawbot69 »

I am having big problems with XPATH in Firefox.... it doesn't work on XML trees that have capital letters - I have this XML:

<bf>
<SME>
...
</SME>
</bf>

I can get SME if i do /bf/*[1] but i cant get it using /bf/SME

anyone have a solution?!!?!?
Post Reply