How do I get rid of the xml:base attribute that is added to my xml document after using xinclude?

StackOverflow https://stackoverflow.com/questions/17352597

I am trying to unmarshal an xml file to a java object using xinclude. I have a schema based on my jaxb-annotated code files. Here is my unmarshalling code:

@Override
public T readFromReader(final Reader reader) throws Exception {
    final Unmarshaller unmarshaller = createUnmarshaller();

    final SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setXIncludeAware(true);
    spf.setNamespaceAware(true);
    //spf.setValidating(true);

    final XMLReader xr = spf.newSAXParser().getXMLReader();
    final InputSource inputSource = new InputSource(reader);
    xr.parse(inputSource)

    final SAXSource source = new SAXSource( xr, new InputSource(reader) );


    try {
        final T object = (T) unmarshaller.unmarshal(source);
        postReadSetup(object);
        return  object;
    } catch (final Exception e) {
        throw new RuntimeException("Cannot parse XML: Additional information is attached. Please ensure your XML is valid.", e);
    }
}

Since I'm calling xr.parse(), the actual unmarshalling is not successful (because the parse closes the stream). However, I am getting a parsing error [Error] file-name.xml:2:66: cvc-complex-type.3.2.2: Attribute 'xml:base' is not allowed to appear in element 'element name'.

I figured out that this is because xinclude always adds an xml:base attribute to an included element to help preserve the base uri. The problem is that my schema doesn't allow this attribute. Is there any way for me to get rid of this? I've looked around online and haven't really seen anything that can be easily implemented.

有帮助吗?

解决方案

I added the following line of code:

// to get rid of xml:base that is brought in with xinclude
spf.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false); 

in the same place that I set the parser factory to be xinclude aware per this article

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top