【问题标题】:How do I get rid of the xml:base attribute that is added to my xml document after using xinclude?如何摆脱使用 xinclude 后添加到我的 xml 文档中的 xml:base 属性?
【发布时间】:2023-03-14 06:02:01
【问题描述】:

我正在尝试使用 xinclude 将 xml 文件解组为 java 对象。我有一个基于我的 jaxb 注释代码文件的模式。这是我的解组代码:

@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);
    }
}

由于我调用了 xr.parse(),实际的解组是不成功的(因为解析关闭了流)。但是,我收到解析错误 [Error] file-name.xml:2:66: cvc-complex-type.3.2.2: Attribute 'xml:base' is not allowed to appear in element 'element name '

我发现这是因为 xinclude 总是将 xml:base 属性添加到包含的元素以帮助保留基本 uri。问题是我的架构不允许这个属性。有什么办法让我摆脱这个吗?我在网上看了一圈,还没有真正看到任何可以轻松实现的东西。

【问题讨论】:

    标签: xml xml-parsing jaxb xinclude


    【解决方案1】:

    我添加了以下代码行:

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

    在我将解析器工厂设置为每个 this article 的 xinclude 感知的同一位置

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      相关资源
      最近更新 更多