1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """XML utilities.
20
21 This module contains useful functions for parsing and using XML data. For the
22 moment, there is only one function that can parse the data inside a processing
23 instruction and return a Python dictionary.
24
25
26
27
28 """
29 __docformat__ = "restructuredtext en"
30
31 import re
32
33 RE_DOUBLE_QUOTE = re.compile('([\w\-\.]+)="([^"]+)"')
34 RE_SIMPLE_QUOTE = re.compile("([\w\-\.]+)='([^']+)'")
35
37 """
38 Utility function that parses the data contained in an XML
39 processing instruction and returns a dictionary of keywords and their
40 associated values (most of the time, the processing instructions contain
41 data like ``keyword="value"``, if a keyword is not associated to a value,
42 for example ``keyword``, it will be associated to ``None``).
43
44 :param pi_data: data contained in an XML processing instruction.
45 :type pi_data: unicode
46
47 :returns: Dictionary of the keywords (Unicode strings) associated to
48 their values (Unicode strings) as they were defined in the
49 data.
50 :rtype: dict
51 """
52 results = {}
53 for elt in pi_data.split():
54 if RE_DOUBLE_QUOTE.match(elt):
55 kwd, val = RE_DOUBLE_QUOTE.match(elt).groups()
56 elif RE_SIMPLE_QUOTE.match(elt):
57 kwd, val = RE_SIMPLE_QUOTE.match(elt).groups()
58 else:
59 kwd, val = elt, None
60 results[kwd] = val
61 return results
62