XPath for indexed node in an XML document - xpath

I have the below document. I am trying to get the test:Category at ABCSite[0].
Tried the XPath $ABCCustomerHierarchy(//ata:ABCSite[0])/test:Customer/test:CustomerID/test:Category, but I got some XPath exception.
Can anybody tell me the right XPath here please?
<test:ABCCustomerHierarchy xmlns:test="http://wwwin.test.com/testschema">
<test:ABCSite>
<test:Customer>
<test:CustomerID>
<test:ID>110984181</test:ID>
<test:Category>ABC_Customer</test:Category>
</test:CustomerID>
</test:Customer>
<test:CustomerReferences>
<test:CustomerID>
<test:ID>17808</test:ID>
<test:Category>KLM_CUSTOMER</test:Category>
</test:CustomerID>
<test:CustomerID>
<test:ID>17808</test:ID>
<test:Category>XYZ_Customer</test:Category>
</test:CustomerID>
<test:CustomerID>
<test:ID>17808</test:ID>
<test:Category>PQR_CUSTOMER</test:Category>
</test:CustomerID>
</test:CustomerReferences>
</test:ABCSite>
<test:ABCSite>
<test:Customer>
<test:CustomerID>
<test:ID>17808</test:ID>
<test:Category>XYZ_Customer</test:Category>
</test:CustomerID>
</test:Customer>
<test:CustomerReferences>
<test:CustomerID>
<test:ID>17808</test:ID>
<test:Category>PQR_CUSTOMER</test:Category>
</test:CustomerID>
</test:CustomerReferences>
</test:ABCSite>
</test:ABCCustomerHierarchy>

You can try :
//ABCCustomerHierarchy/ABCSite[1]/Customer/CustomerID/Category

You should always specify the namespace (test for your example)
Another point is that arrays start with 1 not zero
If you want to get the value of that node, use text():
//test:ABCSite[1])/test:Customer/test:CustomerID/test:Category/text()
To match with exact value use:
//test:ABCSite[1])/test:Customer/test:CustomerID/test:Category[text()='ABC_Customer']

Related

How to take xpath to Get Text from class inside th

I have the following XPath :
//table[#class='ui-jqgrid-htable']/thead/tr/th//text()
And I'm trying to get the text from it with the following command :
String LabelName = driver.findElement(By.xpath("//table[#class='ui-jqgrid htable']/thead/tr/th//text()")).getText()
But it's not printing text, the result is blank. Could you help me please ?
The text() in your xpath does not qualify as an element. Your element ends at //table[#class='ui-jqgrid-htable']/thead/tr/th. Try using getText() for this XPath.
Also, a table would have many headers. Using findElement will only return the first one.
If you want to get all headers use
driver.findElements(By.xpath("//table[#class='ui-jqgrid-htable']/thead/tr/th"))
and loop through the list to getText of individual element.

Trying to find all nodes that has no "modifier" children with & at all

Xpath puzzle
/parameter_list/parameter[//modifier[.='&']]
returns some results
This does not return any result
//parameter_list/parameter[not[//modifier[.='&']]]
It should.
I am trying to find all parameters that has no modifier descendants with & at all
.
fix:
//parameter_list/parameter[not(.//modifier[.='&'])]

xpath expression to read value based on value of sibling

I've below xml and would like to read the value of 'Value' tag whose Name matches 'test2'. I'm using the below xpath , but did not work. Can someone help.
/*[ local-name()='OutputData']/*[ local-name()='OutputDataItem']/*[ local-name()='Name'][normalize-space(.) = 'test2']//*[local-name()='Value']/text()
<get:OutputData>
<get:OutputDataItem>
<get:Name>test1</get:Name>
<get:Value/>
</get:OutputDataItem>
<get:OutputDataItem>
<get:Name>test2</get:Name>
<get:Value>B5B4</get:Value>
</get:OutputDataItem>
<get:OutputDataItem>
<get:Name>test3</get:Name>
<get:Value/>
</get:OutputDataItem>
<get:OutputDataItem>
<get:Name>OP_VCscEncrptCd_VAR</get:Name>
<get:Value/>
</get:OutputDataItem>
</get:OutputData>
Thanks
You were close, but because the get:name and get:value are siblings, you need to adjust your XPath a little.
Your XPath was attempting to address get:value elements that were descendants of get:name, rather than as siblings. Move the criteria that is filtering the get:name into a predicate, then step down into the get:value:
/*[ local-name()='OutputData']/*[ local-name()='OutputDataItem']
[*[ local-name()='Name'][normalize-space(.) = 'test2']]/*[local-name()='Value']/text()
You could also combine the criteria of the predicate filter on the get:name and use an and:
/*[ local-name()='OutputData']/*[ local-name()='OutputDataItem']
[*[ local-name()='Name' and normalize-space(.) = 'test2']]/*[local-name()='Value']/text()
This should work I think:
//*[local-name()="get:Name" and text()="test2"]/following-sibling::*[local-name()="get:Value"]/text()

How do I get the text from an element that has two attributes?

I have a code such as:
<out:a>
<out:Name Type="First" TypeCode="Best">JAE</out:Name>
</out:a>
When I gave the xpath expression as
//*[name()='out:Name'],
I got the result as
<out:Name Type="First" TypeCode="Best" xmlns:out3="abc" xmlns:out2="def" xmlns:out1="ghi" xmlns:out="jkl">JAE</out:Name>
I would like to get the value JAE using xpath expression. Could someone help me in that please?
Add text() on the end:
//*[name()='out:Name']/text()
Or
//out:Name/text()
It depends on the tool you use.
With java / Xpath and evaluate, your xpath expression works well:
expression=" //*[name()='out:Name']";
String value = xPath.evaluate(expression, document);
System.out.println("EVALUATE:"+value); // => EVALUATE:JAE

XPath query to match depending on combinations of child elements

Given the following XML Snippet
<Events>
<Event>
<DateTime>22.09.2009 11:27:18</DateTime>
<EventType>Download</EventType>
</Event>
What is the XPath query to return all Events created today of type download?
/Events/Event[starts-with(DateTime, '22.09.2009') and EventType='Download']
Since I assume that this is a follow-up to your previous question, you might want to use this snippet instead of SelectSingleNode to get all events in a file (if there can be multiple):
foreach (XPathNavigator node in doc.CreateNavigator().Select(expression)) {
// matching node found in document; will process all matching nodes
}
//Events/Event[contains(DateTime,'22.09.2009') and EventType='Download']
/Events/Event[substring(DateTime, 0, 10)='22.09.2009' and EventType='Download']

Resources