Optional item in XPath

How to make b and a optional in the following expression?

 //td[@class='ttr_interest']/b/a/text() 

Basically, /b/a may or may not appear in the tree (only a or only b or both or both of them may be present). How do I specify optional items?

I want to write the text enclosed in td , regardless of whether this text is added to <a> and <b> .

Sample as requested

 <td> <a href="#">text_to_capture</a> </td> <td> <b><a href="#">text_to_capture</a></b> </td> <td> text_to_capture </td> 
+4
source share
3 answers

Using

  (//td[@class='ttr_interest'] | //td[@class='ttr_interest']/a | //td[@class='ttr_interest']/b/a ) /text() 

This selects any node chile text of any element selected by one of the three XPath expressions that are combined together in brackets.

+2
source

EDIT: after comments changed xpath to ask a question

 <bar> xxxx <foo>xxx</foo> <barfoo> <foo>xxx</foo> </barfoo> </bar> 

Use this xpath

 //bar//*/text()|//bar/text() 
0
source

You are not saying in what context you are doing this (XSLT?), But here is the Python / lxml suggestion:

 from lxml import etree XML = """ <root> <td> <a href="#">text_to_capture</a> </td> <td> <b><a href="#">text_to_capture</a></b> </td> <td> text_to_capture </td> </root>""" doc = etree.fromstring(XML) expr = "//td//text()" texts = doc.xpath(expr) print texts # includes whitespace-only nodes for t in texts: if t.strip(): print t.strip() 

Output:

 ['\n ', 'text_to_capture', '\n ', '\n ', 'text_to_capture', '\n ', '\n text_to_capture\n '] text_to_capture text_to_capture text_to_capture 

This solution selects all the text in <td> regardless of the names of the <td> child elements.

0
source

Source: https://habr.com/ru/post/1399631/


All Articles