简体   繁体   English

更改通过xpath查询查询的xml属性的值?

[英]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. 我使用javascript xpath查询(document.evaluate(...))来读取和修改xml / svg / html文档的一部分。

Setting the nodeValue of queried element and text nodes is no problem. 设置查询元素和文本节点的nodeValue没问题。 but when setting attribute values, it is indeed set, but not reflected on the attribute DOM Node. 但是在设置属性值时,它确实已设置,但未反映在属性DOM节点上。

It looks like xpath queries for attribute nodes return (name,value) pairs and not the attribute node. 看起来属性节点返回(名称,值)对而不是属性节点的xpath查询。

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代码,并且能够在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");
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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