Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
CaosDB Crawler
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
CaosDB Crawler
Commits
a6c1fcaf
Commit
a6c1fcaf
authored
9 months ago
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
TST: added xpath tests
parent
3e8b13df
No related branches found
No related tags found
2 merge requests
!181
Release 0.9.0
,
!174
XML Converter
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caoscrawler/xml_converter.py
+20
-2
20 additions, 2 deletions
src/caoscrawler/xml_converter.py
unittests/test_xml_converter.py
+28
-18
28 additions, 18 deletions
unittests/test_xml_converter.py
with
48 additions
and
20 deletions
src/caoscrawler/xml_converter.py
+
20
−
2
View file @
a6c1fcaf
...
...
@@ -39,7 +39,7 @@ from .stores import GeneralStore, RecordStore
from
.structure_elements
import
(
BooleanElement
,
DictElement
,
Directory
,
File
,
FloatElement
,
IntegerElement
,
JSONFile
,
ListElement
,
NoneElement
,
StructureElement
,
TextElement
,
XMLTagElement
,
XMLTextNode
)
TextElement
,
XMLTagElement
,
XMLTextNode
,
XMLAttributeNode
)
from
.utils
import
has_parent
import
lxml.etree
...
...
@@ -93,6 +93,13 @@ class XMLTagConverter(Converter):
- When attribs_as_children is set to true, attribute nodes will be generated from the attributes
of the matched tags.
Namespaces
----------
The default is to take the namespace map from the current node and use it in xpath queries.
Because default namespaces cannot be handled by xpath, it is possible to remap the default namespace
using the key
"
default_namespace
"
.
The key
"
nsmap
"
can be used to define additional nsmap entries.
"""
if
not
isinstance
(
element
,
XMLTagElement
):
raise
TypeError
(
"
Element must be an instance of XMLTagElement.
"
)
...
...
@@ -106,6 +113,11 @@ class XMLTagConverter(Converter):
nsmap
[
default_namespace
]
=
nsmap
[
None
]
del
nsmap
[
None
]
# Set additional nsmap entries from the converter definition:
if
"
nsmap
"
in
self
.
definition
:
for
key
,
value
in
self
.
definition
[
"
nsmap
"
].
items
():
nsmap
[
key
]
=
value
xpath
=
self
.
definition
.
get
(
"
xpath
"
,
"
child::*
"
)
children
=
element
.
tag
.
xpath
(
xpath
,
namespaces
=
nsmap
)
el_lst
=
[]
...
...
@@ -113,7 +125,13 @@ class XMLTagConverter(Converter):
if
isinstance
(
el
,
str
):
raise
RuntimeError
(
"
Only standard xml nodes are supported as results of xpath queries.
"
)
elif
isinstance
(
el
,
lxml
.
etree
.
_Element
):
el_lst
.
append
(
XMLTagElement
(
el
))
if
self
.
definition
.
get
(
"
tags_as_children
"
,
True
):
el_lst
.
append
(
XMLTagElement
(
el
))
if
self
.
definition
.
get
(
"
attribs_as_children
"
,
False
):
for
attrib
in
el
.
attrib
:
el_lst
.
append
(
XMLAttributeNode
(
el
,
attrib
))
if
self
.
definition
.
get
(
"
text_as_children
"
,
False
):
el_lst
.
append
(
XMLTextNode
(
el
))
else
:
raise
RuntimeError
(
"
Unsupported child type.
"
)
return
el_lst
...
...
This diff is collapsed.
Click to expand it.
unittests/test_xml_converter.py
+
28
−
18
View file @
a6c1fcaf
...
...
@@ -249,35 +249,45 @@ def test_namespace_xml(converter_registry):
ok
</node3>
</node2>
2
<test:node2>
sep
</test:node2>
3
</node1>
</root>
"""
xpaths
=
(
"
default:node1/text()
"
,
)
for
xpath
in
xpaths
:
converter
=
XMLTagConverter
(
yaml
.
safe_load
(
"""
type: XMLTag
match_tag:
\\
{{default-namespace
\\
}}root
xpath:
"
{}
"
conv_tail
=
"""
default_namespace: default
subtree:
Text:
type: XMLTextNode
match: (?P<result>.*)
"""
.
format
(
xpath
)),
"
TestXMLTagConverter
"
,
converter_registry
)
"""
tag
=
XMLTagElement
(
fromstring
(
xml_text
))
m
=
converter
.
match
(
tag
)
assert
m
is
not
None
converter
=
XMLTagConverter
(
yaml
.
safe_load
(
"""
type: XMLTag
match_tag:
"
{default-namespace}root
"
xpath:
"
default:node1/text()
"
"""
+
conv_tail
),
"
TestXMLTagConverter
"
,
converter_registry
)
general_store
=
GeneralStore
()
children
=
converter
.
create_children
(
general_store
,
tag
)
assert
len
(
children
)
==
4
tag
=
XMLTagElement
(
fromstring
(
xml_text
))
m
=
converter
.
match
(
tag
)
assert
m
is
not
None
with
pytest
.
raises
(
RuntimeError
,
match
=
"
Only standard xml nodes.*
"
):
converter
.
create_children
(
GeneralStore
(),
tag
)
converter
=
XMLTagConverter
(
yaml
.
safe_load
(
"""
type: XMLTag
match_tag:
"
{default-namespace}root
"
xpath:
"
default:node1
"
attribs_as_children: false
text_as_children: true
tags_as_children: false
"""
+
conv_tail
),
"
TestXMLTagConverter
"
,
converter_registry
)
children
=
converter
.
create_children
(
GeneralStore
(),
tag
)
assert
len
(
children
)
==
2
assert
children
[
0
].
name
==
"
{default-namespace}node1[1]/text()
"
assert
children
[
0
].
value
.
strip
()
==
"
Bla
"
assert
children
[
1
].
name
==
"
{default-namespace}node1[2]/text()
"
assert
children
[
1
].
value
.
strip
()
==
"
text
"
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment