| 1 |
Quickstart |
| 2 |
========== |
| 3 |
|
| 4 |
[](https://github.com/tomschr/getentities/actions/workflows/pythonpackage.yml) |
| 5 |
|
| 6 |
The script `getentityname.py` extracts all referenced entities of a XML files. |
| 7 |
|
| 8 |
|
| 9 |
Why? |
| 10 |
---- |
| 11 |
|
| 12 |
Although most XML parsers can retrieve the information from the DOCTYPE, |
| 13 |
parser APIs are mostly missing access to the *internal subset* of the DTD. |
| 14 |
|
| 15 |
An internal subset is everything between the square brackets: |
| 16 |
|
| 17 |
```xml |
| 18 |
<!DOCTYPE book PUBLIC |
| 19 |
"-//OASIS//DTD DocBook XML V4.4//EN" |
| 20 |
"http://www.docbook.org/xml/4.4/docbookx.dtd" |
| 21 |
[ |
| 22 |
<!-- The external subset --> |
| 23 |
<!ENTITY % entities SYSTEM "entity-decl.ent"> |
| 24 |
%entities; |
| 25 |
<!ENTITY % foo SYSTEM "foo.ent"> |
| 26 |
%foo; |
| 27 |
]> |
| 28 |
<book> <!-- ... --> </book> |
| 29 |
``` |
| 30 |
|
| 31 |
|
| 32 |
Design |
| 33 |
------ |
| 34 |
|
| 35 |
The script performs the following steps: |
| 36 |
|
| 37 |
1. Parse the XML file with the SAX parser to make sure it's well-formed. |
| 38 |
This makes it a little bit easier to parse for corner cases. |
| 39 |
Additionally, the following steps can be made easier as we can expect a |
| 40 |
well-formed XML file. |
| 41 |
1. Read the DOCTYPE header and identify the internal subset of the DTD. |
| 42 |
1. Remove XML comments from the internal subset. |
| 43 |
1. Identify parameter entities in the internal subset. |
| 44 |
1. Load the file and search for other parameter entities. |
| 45 |
1. Return all found parameter entities back to the user. |
| 46 |
|
| 47 |
|
| 48 |
How to run it |
| 49 |
------------- |
| 50 |
|
| 51 |
Applying the script to the above XML file returns: |
| 52 |
|
| 53 |
``` |
| 54 |
$ getentityname.py XMLFILE.xml |
| 55 |
entity-decl.ent foo.ent |
| 56 |
``` |
| 57 |
|