-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripedXmlToTurtle.py
221 lines (205 loc) · 6.31 KB
/
stripedXmlToTurtle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from argparse import ArgumentParser, ArgumentTypeError
from lxml import etree
from pathlib import Path
from rdflib import BNode, Graph, Literal, Namespace
from rdflib.namespace import RDF
from typing import Union
global_cfg = None
def is_valid_file(path: str) -> Path:
"""Checks if given path is a file or not.
Returns pathlib.Path object or raises ArgumentTypeError
if not a valid path to a file.
"""
pathObj = Path(path)
if pathObj.is_file():
return pathObj
else:
raise ArgumentTypeError(f"{path} is not a file")
def init_argparse() -> ArgumentParser:
"""Initializes ArgumentParser object."""
ps = ArgumentParser(
description="""
Converts striped XML to Turtle format.
""")
ps.add_argument(
"xmlFile",
help="Path to XML file to be converted",
type=is_valid_file
)
ps.add_argument(
"--serializePath",
help="""
Path to where the serialized graph should be saved.
Graph is printed if not provided.
"""
)
ps.add_argument(
"--outputFormat",
default="turtle",
const="turtle",
nargs="?",
choices=(
"xml", "n3", "turtle",
"nt", "pretty-xml", "trig",
"json-ld", "hext"
),
help="""
The format that the graph should be output as.
rdflib built-in choices include "xml", "n3", "turtle" (default),
"nt", "pretty-xml", "trig", "json-ld", and "hext".
"""
)
ps.add_argument(
"--collectAttributes",
action="store_true",
help="""
Collect attributes from the XML document if set.
Attributes take on the namespace of the tag they were found in.
"""
)
ps.add_argument(
"--noIgnoreWhitespace",
action="store_false",
help="""
Do not ignore whitespace. Prettified XML documents have
whitespace in them and will be converted if not ignored.
"""
)
ps.add_argument(
"--defaultNamespace",
nargs=2,
default=("ex", "http://example.org/#"),
help="""
Set a default prefix and namespace to be used if namespaces
are not found in the XML document.
Default prefix and URI are 'ex' and 'http://example.org/#'.
"""
)
return ps
def populate_namespaces(nsMap: dict, namespaces: dict, graph: Graph) -> None:
"""
Populate the namespaces dictionary with a
namespace URI to rdflib.Namespace mapping
and binds the created rdflib.Namespace to the graph.
"""
for prefix, uri in nsMap.items():
correctURI = uri
if uri[-1] != "#":
correctURI = uri + "#"
namespaces[uri] = Namespace(correctURI)
graph.bind(prefix, namespaces[uri], override=False)
def parse_qname(element) -> Union[str, str]:
"""Parse QName string from lxml element.
Returns a tuple of strings, the namespace URI and element name.
"""
elQName = etree.QName(element)
return (elQName.namespace, elQName.localname)
def generate_triple(
currentElement,
parentElement,
parentNode: BNode,
isNode: bool,
namespaces: dict,
graph: Graph) -> \
None:
"""Generate triple from current element."""
namespaceURI, elName = parse_qname(currentElement)
if namespaceURI not in namespaces:
populate_namespaces(currentElement.nsmap, namespaces, graph)
namespace = namespaces.get(namespaceURI)
if namespace is None:
namespace = namespaces.get(global_cfg.defaultNamespace[1])
if global_cfg.collectAttributes:
for attribute, value in currentElement.attrib.items():
if not attribute.isalnum():
continue
graph.add((
parentNode,
namespace[attribute],
Literal(value)
))
if isNode:
currentNode = BNode()
if parentNode and parentElement is not None:
parentNSURI, parentElName = parse_qname(parentElement)
if parentNSURI not in namespaces:
populate_namespaces(parentElement.nsmap, namespaces, graph)
parentNamespace = namespaces.get(parentNSURI)
if parentNamespace is None:
defaulNamespaceURI = global_cfg.defaultNamespace[1]
parentNamespace = namespaces.get(defaulNamespaceURI)
if len(currentElement) == 0:
graph.add((
parentNode,
parentNamespace[parentElName],
Literal(currentElement.text)
))
return
graph.add((
parentNode,
parentNamespace[parentElName],
currentNode
))
graph.add((
currentNode,
RDF.type,
namespace[elName]
))
for childElement in currentElement:
generate_triple(
childElement,
currentElement,
currentNode,
not isNode,
namespaces,
graph
)
if not isNode:
if currentElement.text:
graph.add((
parentNode,
namespace[elName],
Literal(currentElement.text)
))
for childElement in currentElement:
generate_triple(
childElement,
currentElement,
parentNode,
not isNode,
namespaces,
graph
)
def convert_xml_and_serialize(filePath: str) -> None:
"""Read XML, create triples, and serialize."""
xmlParser = etree.XMLParser(
remove_comments=True,
no_network=True,
remove_blank_text=global_cfg.noIgnoreWhitespace
)
tree = etree.parse(filePath, xmlParser)
graph = Graph()
namespaces = {}
defaultPrefix = global_cfg.defaultNamespace[0]
defaltNamespaceURI = global_cfg.defaultNamespace[1]
populate_namespaces(
{defaultPrefix: defaltNamespaceURI},
namespaces,
graph
)
generate_triple(
tree.getroot(),
None,
None,
True,
namespaces,
graph
)
print(graph.serialize(
destination=global_cfg.serializePath,
format=global_cfg.outputFormat
))
if __name__ == "__main__":
parser = init_argparse()
global_cfg = parser.parse_args()
convert_xml_and_serialize(global_cfg.xmlFile)