Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list[0] mangles XPath results #3

Open
sjehuda opened this issue Oct 21, 2016 · 0 comments
Open

list[0] mangles XPath results #3

sjehuda opened this issue Oct 21, 2016 · 0 comments
Labels

Comments

@sjehuda
Copy link
Owner

sjehuda commented Oct 21, 2016

Consider using method str.join(iterable).

descriptions = node.xpath('.//p/descendant-or-self::text()')
description = '' if len(descriptions) == 0 else '<br>'.join(descriptions)

Here is where the problem currently relies.

descriptions = node.xpath('.//p/descendant-or-self::text()')
description = '' if len(descriptions) == 0 else descriptions[0]

Our example target is this <p>We are currently looking at a node with a<b> child node </b>within it.</p>, and we would use .//p/descendant-or-self::text() to catch anything between <p> and </p>;

Once we would print list, we would see
[u'We are currently looking at a node with a ', u'child node', u' within it.', u'.'];

This means that list[0] contains We are currently looking at a node with a, list[1] contains child node, and list[2] contains within it.;

list content
list[0] We are currently looking at a node with a
list[1] child node
list[2] within it.

By obtaining only list[0], we would corrupt the desired XPath result by outputing only the content between the first <p> and the first <b>.

This also significantly hardens the use of XPath String Functions such as substring-before, substring-after and substring, and may require us to make an excessive use of for loops, considering the functions that XPath already provides.

@sjehuda sjehuda added the bug label Oct 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant