Xpath - 如何获取元素的所有属性名称和值
我在java中使用xpath。我想获取元素的所有属性(名称和值)。我找到了获取元素属性值的查询,现在我想单独获取属性名称或在单个查询中获取名称和值。
<Element1 ID="a123" attr1="value1" attr2="value2" attr3="value3" attr4="value4" attr5="value5" />
这里使用以下查询来获取 Element1
的所有属性值 XmlUtils.getAttributes(Path, String.format("//*/@*"));
使用这种格式 //*/@*
我可以获取值。结果将是 value1 value2 value3 value4 value5 a123
现在我想知道查询以获取所有属性名称,或查询以获取所有属性名称和值。
I am using xpath in java. I want to get all the attributes (name & Value) of an element. I found the query to get the attribute values of an element, now I want to get attribute names alone or names and values in single query.
<Element1 ID="a123" attr1="value1" attr2="value2" attr3="value3" attr4="value4" attr5="value5" />
Here using the following query to get all the attribute values of Element1
XmlUtils.getAttributes(Path, String.format("//*/@*"));
Using this format //*/@*
I can get the values. result would be value1 value2 value3 value4 value5 a123
Now I want to know the query to get all the attribute names, or query to get all the attributes name and value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要选择名为 Element1 的文档中所有元素的所有属性:
//Element1/@*
。这将返回一个包含属性节点的节点集。然后您可以迭代节点集。如果您已经有一个上下文节点并希望在其下查找结果,则查询将为
.//Element1/@*
。这通常比查询整个文档更有效。使用 getElementsByTagName。
要获取特定元素的属性,请迭代其 attributes 属性。
To select all attributes of all elements in the document named Element1:
//Element1/@*
. This will return a nodeset containing attribute nodes. You can then iterate the nodeset.If you already have a context node and wish to find results under it, the query would be
.//Element1/@*
. This is usually more efficient than querying the entire document.And it may be more efficient to find all elements of a given name using getElementsByTagName.
To get the attributes of a particular element, iterate its attributes property.
我必须在 Oracle Service Bus 中执行此操作,并且必须仅使用 xPath 来创建缓存键,适合我的解决方案是:
I had to do it in Oracle Service Bus and had to do using only xPath to create a cache key and the solution that work for me was: