Java XML: Avoid relative systemId expansion against user.dir

50 Views Asked by At

Consider the following example XML:

<book xmlns:xlink="http://www.w3.org/1999/xlink"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="xsd/book.xsd" />

When parsing this xml file with standard JAXP APIs (which will often use a Xerces implementation), the "xsd/book.xsd" systemId will get "resolved" against the user directory and will result in file:///home/user/xsd/book.xsd.

For xerces, this behavior is implemented here: https://github.com/apache/xerces2-j/blob/cf0c517a41b31b0242b96ab1af9627a3ab07fcd2/src/org/apache/xerces/impl/XMLEntityManager.java#L1894

To workaround this, we're currently using an EntityResolver2 to extract the original, relative systemId out of the absolute URI file:///home/user/xsd/book.xsd but this is really hacky.

Question:
Is there better way, e.g. by disabling this strange "userdir"-behavior and just keep the relative systemIds as they are?

1

There are 1 best solutions below

4
Michael Kay On

If you want the schemaLocation to be interpreted as relative to the base URI of the source document, just make sure that the base URI of the source document is known to Xerces. For example, don't supply the input as a FileInputStream with no known system Id. It will only use the current directory as a fallback if it doesn't know where the input file is located.