Skip to content

Commit

Permalink
Merge pull request #28 from mluis7/pxx-19
Browse files Browse the repository at this point in the history
pxx-19
  • Loading branch information
mluis7 authored Dec 20, 2024
2 parents 36248a5 + 5ac4194 commit 3589c4c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/dist/
src/*.egg-info
__pycache__/
/.pytest_cache/
23 changes: 15 additions & 8 deletions .pydevproject
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>





<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">




<path>/${PROJECT_DIR_NAME}/src</path>




<path>/${PROJECT_DIR_NAME}/tests</path>




</pydev_pathproperty>





<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.9</pydev_property>



<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">python3.9</pydev_property>


<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>




</pydev_project>
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Table of contents
* [Method parse(...)](#method-parse)
* [Print result modes](#print-result-modes)
* [HTML support](#html-support)
* [Relative expressions](#relative-expressions)
* [Unqualified vs. Qualified](#unqualified-vs-qualified)
* [Initial Xpath Examples](#initial-xpath-examples)
* [Performance](#performance)
Expand Down Expand Up @@ -215,6 +216,68 @@ or on command line
pyxml2xpath tests/resources/html5-small.html.xml 'all' '//*[@id="math"]'
```

## Relative expressions
Build relative expressions when passing `xpath_base` kword argument. The xpath of the parent should be removed so `base_xpath` should be like:

`xpath_base = '//*[@id="math"]/parent::* | //*[@id="math"]/descendant-or-self::*'`

Example:

```python
from lxml import html
from xml2xpath import xml2xpath

filepath = 'tests/resources/html5-small.html.xml'
hdoc = html.parse(filepath)

needle = 'math'
xpath_base = f'//*[@id="{needle}"]/parent::* | //*[@id="{needle}"]/descendant-or-self::*'
xmap = xml2xpath.parse(None, itree=hdoc, xpath_base=xpath_base)[2]

rel_xpath = []
xiter = iter(xmap)
# parent xpath
x0 = next(xiter)
# base element xpath
x1 = next(xiter)
# get base element attributes and build a predicate with first
x1a = ''
if len(xmap[x1][2]) > 0:
x1a = f'[@{xmap[x1][2][0]}="{needle}"]'
# base element relative xpath (/html/body/math -> //math)
x1f = x1.replace(x0, '/')
# remove numeric indexes if any (div[1] -> div)
x1f = x1f.split('[', 1)[0]
# add first attribute as predicate
x1f += x1a
rel_xpath.append(x1f)

# children relative xpath
for xs in list(xmap.keys())[2:]:
rel_xpath.append(xs.replace(x1, x1f))

for x in rel_xpath:
print(x)
```

Output

```None
//math[@id='math']
//math[@id='math']/mrow
//math[@id='math']/mrow/mi
//math[@id='math']/mrow/mo
//math[@id='math']/mrow/mfrac
//math[@id='math']/mrow/mfrac/mn
//math[@id='math']/mrow/mfrac/msqrt
//math[@id='math']/mrow/mfrac/msqrt/mrow
//math[@id='math']/mrow/mfrac/msqrt/mrow/msup
//math[@id='math']/mrow/mfrac/msqrt/mrow/msup/mi
//math[@id='math']/mrow/mfrac/msqrt/mrow/msup/mn
//math[@id='math']/mrow/mfrac/msqrt/mrow/mo
//math[@id='math']/mrow/mfrac/msqrt/mrow/mn
```

## Unqualified vs. Qualified
Symbolic element tree of `tests/resources/wiki.xml` showing position of unqualified elements.

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.3
0.3.4
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ dynamic = ["version"]
dependencies = [
"lxml"
]
requires-python = ">=3.9, <=11"
requires-python = ">=3.9"
authors = [
{name = "Luis Muñoz", email = "south.minds@gmail.com"}
]

description = "Find xpath expressions from XML document."
description = "Generate xpath expressions from XML document."
readme = "README.md"
license = {file = "LICENSE"}
license = {text = "GPL-3.0"}
keywords = ["xpath", "xml"]
classifiers = [
"Development Status :: 5 - Production/Stable",
Expand Down

0 comments on commit 3589c4c

Please sign in to comment.