简体   繁体   中英

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");
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM