How to get H1,H2,H3,… using a single xpath expression

Use: /html/body/*[self::h1 or self::h2 or self::h3]/text() The following expression is incorrect: //html/body/*[local-name() = “h1” or local-name() = “h2” or local-name() = “h3”]/text() because it may select text nodes that are children of unwanted:h1, different:h2, someWeirdNamespace:h3. Another recommendation: Always avoid using // when the structure of the XML document is statically known. Using // most often … Read more

Retrieve an xpath text contains using text()

text() gets you a set of text nodes. I tend to use it more in a context of //span//text() or something. If you are trying to check if the text inside an element contains something you should use contains on the element rather than the result of text() like this: span[contains(., ‘testuser’)] XPath is pretty … Read more

Does jsoup support xpath?

JSoup doesn’t support XPath yet, but you may try XSoup – “Jsoup with XPath”. Here’s an example quoted from the projects Github site (link): @Test public void testSelect() { String html = “<html><div><a href=”https://github.com”>github.com</a></div>” + “<table><tr><td>a</td><td>b</td></tr></table></html>”; Document document = Jsoup.parse(html); String result = Xsoup.compile(“//a/@href”).evaluate(document).get(); Assert.assertEquals(“https://github.com”, result); List<String> list = Xsoup.compile(“//tr/td/text()”).evaluate(document).list(); Assert.assertEquals(“a”, list.get(0)); Assert.assertEquals(“b”, list.get(1)); } … Read more

Xpath – Selecting attributes using starts-with

I am trying to write an xpath expression that selects all div tags that have an attribute id that start with CompanyCalendar. The following expression is perhaps what you are looking for: //div[starts-with(@id,’CompanyCalendar’)] What it does, in plain English, is Return all div elements in the XML document that have an attribute id whose attribute … Read more

Find sibling node after specified node is found

How can I search for a first k sibling after m node? Basically, find some node and then continue searching from that node. Assuming that we have the following well-formed XML document: <t> <n></n> <k></k> <m></m> <k></k> </t> then the following XPath expression: /*/m[1]/following-sibling::k[1] selects the first k following-sibling of the first m child of … Read more

XML xpath, get the parent element till a specific element

Given the XML <?xml version=”1.0″?> <root> <foo id=”id1″> <foo id=”i2″> <baz/> </foo> </foo> </root> You can find the nearest ancestor foo element from baz using the XPath expression: //baz/ancestor::foo[1] Which will select the foo element node of id “i2”. So in your example (if I understand right) once you have got the “a” element you … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)