I know that I can use xpath to perform joins using the "|" operator. Is there a way to perform semi-joins in xpath like for example:
book[author = article/author]/title
If semi-joins exist, what would the output of the query above look like. Does it just output the title element of each book that has an author who also authored an article?
Maybe you want //book[author = //article/author]/title. With your current attempt book[author = article/author] the article elements would need to be children of the book element which does not seem likely.
The given query would return the title of each book that contains an article that has been authored by that book's author. Thus, in the context of books below, the only thing returned would be the title element with the text "title 0".
<books>
<book>
<title>Title 0</title>
<author>Petri, M</author>
<article>
<title>Title 1</title>
<author>Petri, M</author>
</article>
<article>
<title>Title 2</title>
<author>Butcher, P</author>
</article>
</book>
<book>
<title>Title 3</title>
<author>Butcher, P</author>
<article>
<title>Title 4</title>
<author>Petri, M</author>
</article>
</book>
</books>
Related
I have a xml file like following
<topic>
<title>Abstract
</title>
<body>
<p>
abstract data
</p>
</body>
</topic>
<topic>
<title>Keywords</title>
<body>
<p>
keywords data
</p>
</body>
</topic>
I have to check if title is "Keywords" than show the <p>text in </p>.
can anyone help me to get the exact xpath for this?
Thanks in advance
Try this one and let me know the result:
//title[text()="Keywords"]/following::p
or
//topic[title[text()="Keywords"]]//p
//title[text()="Keywords"]/body/p
for text only
//title[text()="Keywords"]/body/p/text()
please avoid double slash "//" and following, it will travel all the P tag
Try this below xpath
//title[text()="Keywords"]/following::p
Explanation of xpath:- Start your xpath with <title> along with text method and move ahead to the <p> tag using the following keyword.
Given the following XML, I want to get the value "0123456" for Name="Cat":
xml.xpath '//Custom[Name="Cat"]'
Gives me the first custom, which is correct, but I only want the "Value" not the entire Custom node.
<body>
<Custom>
<count>1</count>
<Name>Cat</Name>
<Value>0123456</Value>
</Custom>
<Custom>
<count>2</count>
<Name>Dog</Name>
<Value>9876543</Value>
</Custom>
<body>
I only want the "Value" not the entire Custom node.
So just go on writing the path:
//Custom[Name="Cat"]/Value
I prefer to use CSS selectors over XPath, for readability, as usually CSS contains less visual noise:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<body>
<Custom>
<count>1</count>
<Name>Cat</Name>
<Value>0123456</Value>
</Custom>
<Custom>
<count>2</count>
<Name>Dog</Name>
<Value>9876543</Value>
</Custom>
<body>
EOT
foo = doc.search('name:contains("Cat")').map{ |node|
node.next_element.text
}
foo # => ["0123456"]
This works because Nokogiri contains some of the jQuery CSS extensions, resulting in some useful additions.
To get the value element text you need to set the xpath as below:
doc = Nokogiri::HTML(<<EOT)
<body>
<Custom>
<count>1</count>
<Name>Cat</Name>
<Value>0123456</Value>
</Custom>
<Custom>
<count>2</count>
<Name>Dog</Name>
<Value>9876543</Value>
</Custom>
<body>
EOT
val=doc.xpath("//Custom[Name='Cat']/Value").text()
val => "0123456"
I have an XML structure that looks like this:
<document>
<body>
<section>
<title>something</title>
<subtitle>Something again</subtitle>
<section>
<p xml:id="1234">Some text</p>
</section>
</section>
<section>
<title>something2</title>
<subtitle>Something again2</subtitle>
<section>
<p xml:id="12345678">Some text2</p>
<p getelement="1234"></p>
</section>
</section>
</body>
</document>
I want to search for the attribut value defined in "getelement". I got this code from a friendly soule here:
//section[section/p[#xml:id=#getelement]]/subtitle
but it doesnt work and i cant use current() since it is not supported in Arbortext.
You are comparing the attributes of the same element, but they are not. You have to find the getelement:
//section[section/p[#xml:id=//#getelement]]/subtitle
Also note that xml:id attributes cannot start with digits.
I have an XML-structure that looks like this:
<document>
<body>
<section>
<title>something</title>
<subtitle>Something again</subtitle>
<section>
<p xml:id="1234">Some text</p>
</section>
</section>
<section>
<title>something2</title>
<subtitle>Something again2</subtitle>
<section>
<p xml:id="12345678">Some text2</p>
</section>
</section>
</body>
</document>
What i want to is to find search for the attribute xml:id containing 12345678 and once found, get the previous sibling (subtitle) content. Is this possible with xpath? I have this:
//p[contains(#xml:id,'12345678')]/preceding-sibling::subtitle
If I have understood the post correctly, for the specific query that you have put, the expected answer is Something Again2. You can use the following query to do this:
UPDATED as the document schema is changed
//section[section/p[#xml:id="12345678"]]/subtitle
<html>
apple
<Br>
orange
<br>
drugs
</html>
can you do something like
//html/text()[2]
it doesn't work.
<?xml version="1.0"?>
<html>
apple
<br/>
orange
<br/>
drugs
</html>
//html/text()[2]
returns orange for me # http://www.xmlme.com/XpathTool.aspx. What language are you dealing with?