XPath : Number of Occurrence of an element - xpath

How do i get the output for first tag starting with "<intro><longtitle" as 1 . Second tag "<intro><longtitle>" as 2 and so on using XPATH. The need is to get the occurrence of the element .
<intro><longtitle> Demo </longtitle>
..
..
<intro><longtitle> Test </longtitle>
.
.
<intro><longtitle> Demo Test</longtitle>
Regards,
Sri

For your XML corrected to be well-formed,
<?xml version="1.0" encoding="UTF-8"?>
<r>
<intro>
<longtitle> Demo </longtitle>
</intro>
<intro>
<longtitle> Test </longtitle>
</intro>
<intro>
<longtitle> Demo Test </longtitle>
</intro>
</r>
you can specify the intro element with a Test string value of longtitle:
//intro[normalize-space(longtitle) = 'Test']
and count the preceding siblings,
count(//intro[normalize-space(longtitle) = 'Test']/preceding-sibling::intro) + 1
to determine that the selected intro is the second sibling:
2

Related

Select only the first element 'libro' that don't have the attribute 'paginas'

Given the attached xml file:
<?xml version="1.0" encoding="UTF-8"?>
<biblioteca>
<libro paginas="100">
<titulo>Los bandidos de la playa</titulo>
<autor>Rosario Lopez</autor>
<isbn>1231-123-123-2233</isbn>
<precio>123</precio>
<fechaPublicacion año="1920"/>
</libro>
<libro paginas="200">
<titulo>Indagaciones publicas</titulo>
<autor sexo="M">Aurora Laspitas</autor>
<isbn>1231-222-3333-4444</isbn>
<precio>40</precio>
<fechaPublicacion año="2000"/>
</libro>
<libro>
<titulo>libro barato</titulo>
<autor sexo="H">Cipriano Lopez</autor>
<isbn>1231-2343 32333333</isbn>
<precio>10</precio>
<fechaPublicacion año="1978"/>
</libro>
<libro>
<titulo>libro de ayuda</titulo>
<autor sexo="H">Zacarias Sanchez</autor>
<isbn>1231-2343 32333333</isbn>
<precio>10</precio>
<fechaPublicacion año="1999"/>
</libro>
</biblioteca>
I want to select only the first element 'libro' that doesn't have the attribute 'paginas'.
Here is my try that doesn't work:
/biblioteca/libro[not(#paginas)]/../libro[1]
Thanks
You can use this XPath-1.0 expression:
/biblioteca/libro[not(#paginas)][1]

How to use following in Xpath to get siblings in a Tag

I have following Structure: I am trying to build a robust method to extract the elements of FT1_19_0 of the FT1_19 Tag in the order they appear. However
in my results the elements are rearranged. How can i get my result in correct order.
//*/FT1_19/FT1_19_0[contains(../FT1_19_2,'I10') and
not(.=../following::FT1_19/FT1_19_0)]
The Result(Rearranged)
X50.0XXA
M76.891
M17.11
M23.303
<?xml version="1.0" encoding="UTF-8"?>
<root>
<FT1>
<FT1_1>1</FT1_1>
<FT1_4>20180920130000</FT1_4>
<FT1_5>20180924110101</FT1_5>
<FT1_6>CG</FT1_6>
<FT1_7>99203</FT1_7>
<FT1_9/>
<FT1_10>1.00</FT1_10>
<FT1_13>NPI</FT1_13>
<FT1_16>
<FT1_16_1>Gavin, Matthew, MD</FT1_16_1>
<FT1_16_3>22</FT1_16_3>
</FT1_16>
<FT1_19 NO="1">
<FT1_19_0>M76.891</FT1_19_0>
<FT1_19_2>I10</FT1_19_2>
</FT1_19>
<FT1_19 NO="2">
<FT1_19_0>M17.11</FT1_19_0>
<FT1_19_2>I10</FT1_19_2>
</FT1_19>
<FT1_19 NO="3">
<FT1_19_0>M23.303</FT1_19_0>
<FT1_19_2>I10</FT1_19_2>
</FT1_19>
<FT1_19 NO="4">
<FT1_19_0>X50.0XXA</FT1_19_0>
<FT1_19_2>I10</FT1_19_2>
</FT1_19>
</FT1>
</root>
Use this if you are using java:
List<WebElement> list = driver.findElements(By.xpath("//ft1_19//following::ft1_19_0"));
for(WebElement we:list) {
System.out.println(we.getText());
}

how to read the data from XML with spaces using oracle

I want to read the data from passage_para tag, after passage_para I have 2 spaces before the expression tag and after the expression tag I have one more space, etc. When I use extract function to get the passage_para tag from the XMLTYPE column it is eliminating all the spaces.
<?xml version="1.0" encoding="UTF-8"?> <item> <information number="sdjsadh" > <response_direction delivery_mode="xcs"> <dparagraph>test</dparagraph> </response_direction> </information> <i_content> <stimulus_reference> <passage> <prose style="1"> <passage_para> <expression> <math xmlns="Math" xmlns:xlink="xlink" display="inline" overflow="scroll"> <mr> <mi>z</mi> <mo>></mo> <mn>0</mn> </mr> </math> </expression> </passage_para> </prose> </passage> </stimulus_reference> </i_content> </item>
which I don't want because it is taking out the spaces. The desired output I need is " z > 0 ".
Note: Between the passage_para tag the child nodes may change, they are not going to be the same.

Can't address XML attribute thought XPath in Ruby (using Nokogiri)

I'm trying to filter xml file to get nodes with certain attribute. I can successfully filter by node (ex. \top_manager), but when I try \\top_manager[#salary='great'] I get nothing.
<?xml version= "1.0"?>
<employee xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="employee.xsd">
<top_manager>
<ceo salary="great" respect="enormous" type="extra">
<fname>
Vasya
</fname>
<lname>
Pypkin
</lname>
<hire_date>
19
</hire_date>
<descr>
Big boss
</descr>
</ceo>
<cio salary="big" respect="great" type="intro">
<fname>
Petr
</fname>
<lname>
Pypkin
</lname>
<hire_date>
25
</hire_date>
<descr>
Resposible for information security
</descr>
</cio>
</top_manager>
......
How I need to correct this code to get what I need?
require 'nokogiri'
f = File.open("employee.xml")
doc = Nokogiri::XML(f)
doc.xpath("//top_manager[#salary='great']").each do |node|
puts node.text
end
thank you.
That's because salary is not attribute of <top_manager> element, it is the attribute of <top_manager>'s children elements :
//xmlns:top_manager[*[#salary='great']]
Above XPath select <top_manager> element having any of it's child element has attribute salary equals "great". Or if you meant to select the children (the <ceo> element in this case) :
//xmlns:top_manager/*[#salary='great']

XmlUnit empty Elements

I try to compare two xml with xmlUnit. I have the following problem. When i have two empty elements like the example below xmlUnit identificate the elements as a difference. Can i configure xmlUnit to ignore this?
</name> and <name></name>
I am only interesting in difference like the next two examples.
<name>test1</name> and <name>test2</name>
difference: test1 and test2
or
<name>test1</name> and <name></name>
difference
test1 and ...
My code:
`
Diff diff = new Diff(fr1, fr2);
DetailedDiff detailedDiff = new DetailedDiff(diff);
List differenceList = detailedDiff.getAllDifferences();
List differences = detailedDiff.getAllDifferences();
for (Object object : differences) {
Difference difference = (Difference)object;
String node1;
String node2;
node1 = difference.getControlNodeDetail().getNode().getNodeName() + " " + difference.getControlNodeDetail().getNode().getNodeValue();
node2 = difference.getTestNodeDetail().getNode().getNodeName() + " " + difference.getTestNodeDetail().getNode().getNodeValue();
}
`
Assuming your </name> is a typo and it is <name/> as per the comment,
then you could try the following.
XMLUnit.setIgnoreWhitespace(true);
Seems to work for me.
ie.
When I try to compare <Carp1></Carp1> with <Carp1/>.
Without the above setting, I get
Expected text value '
' but was '
' - comparing <CfgDN ...>
</CfgDN> at /CfgDN[1]/text()[19] to <CfgDN ...>
</CfgDN> at /CfgDN[1]/text()[19]
With the above setting, all is similar and identical.

Resources