Xpath - 如何获取元素的所有属性名称和值

发布于 2024-08-25 19:41:02 字数 462 浏览 2 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北斗星光 2024-09-01 19:41:02

要选择名为 Element1 的文档中所有元素的所有属性://Element1/@*。这将返回一个包含属性节点的节点集。然后您可以迭代节点集。

如果您已经有一个上下文节点并希望在其下查找结果,则查询将为 .//Element1/@*。这通常比查询整个文档更有效。

// input is an InputSource or a DOM node
NodeList nl = (NodeList) xpath.evaluate("//Element1/@*", input, XPathConstants.NODESET);
int length = nl.getLength();
for( int i=0; i<length; i++) {
    Attr attr = (Attr) nl.item(i);
    String name = attr.getName();
    String value = attr.getValue();
}

使用 getElementsByTagName

NodeList nl = document.getElementsByTagName("Element1"); 

要获取特定元素的属性,请迭代其 attributes 属性。

NamedNodeMap nl = element.getAttributes();
int length = nl.getLength();
for( int i=0; i<length; i++) {
    Attr attr = (Attr) nl.item(i);
    String name = attr.getName();
    String value = attr.getValue();
}

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.

// input is an InputSource or a DOM node
NodeList nl = (NodeList) xpath.evaluate("//Element1/@*", input, XPathConstants.NODESET);
int length = nl.getLength();
for( int i=0; i<length; i++) {
    Attr attr = (Attr) nl.item(i);
    String name = attr.getName();
    String value = attr.getValue();
}

And it may be more efficient to find all elements of a given name using getElementsByTagName.

NodeList nl = document.getElementsByTagName("Element1"); 

To get the attributes of a particular element, iterate its attributes property.

NamedNodeMap nl = element.getAttributes();
int length = nl.getLength();
for( int i=0; i<length; i++) {
    Attr attr = (Attr) nl.item(i);
    String name = attr.getName();
    String value = attr.getValue();
}
尝蛊 2024-09-01 19:41:02

我必须在 Oracle Service Bus 中执行此操作,并且必须仅使用 xPath 来创建缓存键,适合我的解决方案是:

concat(
    string-join(//*[string-length(normalize-space(string-join(text(), ''))) > 0]/concat(local-name(), 
                                                                                        ':',
                                                                                        normalize-space(string-join(text(), ''))), '_'), 
    '_',
    string-join(//@*[normalize-space(.) != '']/concat(name(), ':', .), '_')
)

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:

concat(
    string-join(//*[string-length(normalize-space(string-join(text(), ''))) > 0]/concat(local-name(), 
                                                                                        ':',
                                                                                        normalize-space(string-join(text(), ''))), '_'), 
    '_',
    string-join(//@*[normalize-space(.) != '']/concat(name(), ':', .), '_')
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文