changing value of xml attribute queried by xpath query?
I use javascript xpath queries (document.evaluate(...)) to read and modify parts of xml/svg/html documents.
Setting the nodeValue of queried element and text nodes is no problem. but when setting attribute values, it is indeed set, but not reflected on the attribute DOM Node.
It looks like xpath queries for attribute nodes return (name,value) pairs and not the attribute node.
Why is it so?
How开发者_开发技巧 can I circumvent it?
A bit of code would help immensely and what browser are you doing it in? I used this simple HTML code and was able to change the attribute quite happily in FF 3.5.
<html>
<body>
<img src="Jellyfish.jpg"/>
<script>
var node = document.evaluate("//img/@src", document, null, XPathResult.ANY_TYPE, null);
var val = node.iterateNext();
val.textContent = "Desert.jpg";
</script>
</body>
</html>
xPath.compile("//EXPRESSION_TO_FIND_ATTRIBUTE");
NodeList list = XPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < list.getLength(); i++){
list.item(i).setTextContent("ATTRIBUTE_VALUE");
}
精彩评论