3

I am using XML-Simple along with XML-SAX-Expat to parse a document with external entities. I declare my entity like:

<!ENTITY chap1 SYSTEM "chapter-1.xml">

And I refer to from another file in the same directory.

However, it is searching my current working directory for the file, and not the location of the root document.

404 File `/u/egge/chapter-1.xml' does not exist

Handler couldn't resolve external entity at line 35, column 2, byte 1040
error in processing external entity reference at line 35, column 2, byte 1040 at     /perl5/PROJ/XML-Parser/2.36-2.0.1-5.10-64/lib/perl5/XML/Parser.pm line 187

How can I get it to use the root document's directory instead of my CWD?

brianegge
  • 29,240
  • 13
  • 74
  • 99
  • Any reason you can't simple `chdir` to the location of the root document? Failing that, you probably need to include a code sample demonstrating what you're doing. – RickF Oct 14 '13 at 14:56
  • It depends on the environment you are in. Is this Apache? CGI/mod_perl? Your document_root is probably accessible via variables like $r. – KateYoak Oct 15 '13 at 13:47

1 Answers1

2

The utility xmllint has the --noent flag, that will expand the ENTITY references of the root document. If you can't find an alternative, you could always rewrite the input file quickly with this:

xmllint --noent $input.xml > expanded.xml

It also has the advantage of precomputing the full XML file, rather than doing it each time at runtime.

tlrrd
  • 322
  • 3
  • 8