Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath - How to get all the attribute names and values of an element

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.

like image 227
SWDeveloper Avatar asked Mar 17 '10 07:03

SWDeveloper


People also ask

What is text () in XPath?

XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.

What is XPath expression to select all the book elements?

/bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00. /bookstore/book[price>35.00]/title. Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00.


2 Answers

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();
}
like image 110
Lachlan Roche Avatar answered Oct 16 '22 10:10

Lachlan Roche


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(), ':', .), '_')
)
like image 1
Flavio Sousa Avatar answered Oct 16 '22 10:10

Flavio Sousa