Skip to content

Instantly share code, notes, and snippets.

@tomschr
Last active July 10, 2018 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomschr/8d86797ba19c57f41ea2a47535e8b431 to your computer and use it in GitHub Desktop.
Save tomschr/8d86797ba19c57f41ea2a47535e8b431 to your computer and use it in GitHub Desktop.
Incomplete XInclude Resolution for 2nd Level?
#!/usr/bin/env python3
import sys
import textwrap
from lxml import etree
def parse(xmlfile, xinclude=True):
try:
xmlparser = etree.XMLParser(collect_ids=False, recover=False)
tree = etree.parse(xmlfile, parser=xmlparser)
if xinclude:
tree.xinclude()
return 0
except (etree.XMLSyntaxError, etree.XIncludeError) as err:
print("ERROR: %s" % err, file=sys.stderr)
print(textwrap.indent(str(err.error_log), prefix=" "), file=sys.stderr)
return 10
if __name__ == "__main__":
result = parse(sys.argv[1])
sys.exit(result)
<section version="5.1"
xmlns="http://docbook.org/ns/docbook"
xmlns:xi="http://www.w3.org/2001/XInclude">
<title>First Level</title>
<xi:include href="second.xml"/>
</section>
<article version="5.1"
xmlns="http://docbook.org/ns/docbook"
xmlns:xi="http://www.w3.org/2001/XInclude">
<title>Test for nested XIncludes</title>
<xi:include href="first.xml"/>
</article>
<section version="5.1"
xmlns="http://docbook.org/ns/docbook">
<title>Second</title>
<para>The quick brown fox.<!--</para>-->
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment