0

I am trying to debug some inherited code. So there is a line of code elt = doc.xpath('body/div/pre[@id="bb flags"]')[0]

I want to see what the entire document looks like at that point, rather than just a specific piece. So what xpath should I insert there? i.e.

elt_entire_document = doc.xpath(new xpath)
logging.info("The full document here is " + elt_entire_document.text)

Is this even possible with xpath, or is it more complicated than that?

Chozang
  • 178
  • 1
  • 10

2 Answers2

1

Simply like this, based on our comments :

logging.info("The full document here is " + text)
Florian Bernard
  • 2,561
  • 1
  • 9
  • 22
  • Florian, thanks, but that gives me: TypeError: can only concatenate str (not "NoneType") to str If I then change it to logging.warning('td145 doc.text = ' + str(doc.text)) I get doc.text = None Which I know isn't correct as the whole document, as the xpath as it currently is does return something. – Chozang Nov 12 '19 at 19:32
  • 1
    @Tharpa what library do you used? – Florian Bernard Nov 12 '19 at 19:46
  • 1
    My predecessor used etree. doc = etree.HTML(txt). I guess that indirectly answers my question. If I print out txt in the calling method, I have my answer. – Chozang Nov 12 '19 at 19:48
  • Yes, I have updated my anwser accordingly. but you get it. – Florian Bernard Nov 12 '19 at 19:53
0

Your question title seems to be asking about selecting a whole document, yet your question body seems to be asking about displaying a selected node...

Selecting the whole document via XPath

Selecting the whole document might mean any of the following:

  • / selects the root node of XML document.
  • /* selects the document element (aka the root element) of the XML document.
    (Its parent is the root node.)
  • string(/) selects the string-value of the XML document.

See also:


Python pretty print subtree

kjhughes
  • 106,133
  • 27
  • 181
  • 240