Java 中的 XML 验证:processContents="lax"似乎无法正常工作

发布于 2024-12-10 21:04:20 字数 2935 浏览 0 评论 0 原文

我有一个 XML 模式,其中包含许多

<any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded" />

定义,即它允许插入其他名称空间的任意标签。 processContents="lax" 表示解析器应该尝试验证这些标签(如果它具有相应的架构)(1) (2)

对我来说,这意味着,如果我向解析器提供所有架构文档,并且辅助命名空间之一存在无效的 XML 标记,则它需要报告错误。

然而,Java XML 验证器似乎忽略了此类错误。我已经验证解析器具有执行验证所需的所有架构文档(如果我将 XML 架构更改为 processContents="strict",它会按预期工作并使用辅助架构文档进行验证) 。看起来验证器的行为就像使用值 skip 指定属性一样。

用于验证的 Java 代码:

/*
 * xmlDokument is the file name of the XML document
 * xsdSchema is an array with all schema documents
 */
public static void validate( String xmlDokument, Source[] xsdSchema ) throws SAXException, IOException {   
  SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
  Schema schema = schemaFactory.newSchema( xsdSchema );
  Validator validator = schema.newValidator();
  validator.setErrorHandler( new MyErrorHandler() );
  validator.validate( new StreamSource(new File(xmlDokument)) );
}

最小示例:

主要架构:

<xs:schema
    xmlns="baseNamespace"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="baseNamespace"
    xmlns:tns="baseNamespace">

<!-- Define single tag "baseTag" -->
<xs:element name="baseTag">
  <xs:complexType>
    <xs:sequence>
      <xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

辅助架构:

<xs:schema
    xmlns="secondaryNamespace"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="secondaryNamespace"
    xmlns:tns="secondaryNamespace"
    elementFormDefault="qualified"
    attributeFormDefault="qualified">

<xs:element name="additionalTag"/>

</xs:schema>

我正在尝试的 XML 文档验证:

<baseTag
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="baseNamespace"
  xmlns:secondary="secondaryNamespace"
  xsi:schemaLocation="
    baseNamespace base.xsd
    secondaryNamespace secondary.xsd">

  <secondary:additionalTag/>
  <secondary:invalidTag/>
</baseTag>

使用上面的 Java 代码给出两个模式文档不会产生任何验证错误,只有当我将基本模式中的 lax 更改为 strict 时(我不这样做)不想要)。本例中的错误消息是

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'secondary:invalidTag'.

问题:

我是否误解了什么,这实际上是正确的行为吗?或者我对 processContents 的看法是对的吗?

我的架构文档是否在做正确的事情?

我的Java代码正确吗?我怎样才能改变它,使其按预期运行?

I have an XML Schema which contains a number of

<any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded" />

definitions, i.e., it allows to insert arbitrary tags of other namespaces. processContents="lax" indicates that the parser should try do validate these tags, if it has the according schema (1) (2).

For me this means, that if I give the parser all schema documents, and there is an invalid XML tag of one of the secondary namespaces, it needs to report an error.

However, it seems that the Java XML validator ignores such errors. I have verified that the parser has all the necessary schema documents to perform the validation (if I change the XML schema to processContents="strict", it works as expected and uses the secondary schema documents for validation). It seems that for the validator behaves as if the attribute is specified with value skip.

Java code for validation:

/*
 * xmlDokument is the file name of the XML document
 * xsdSchema is an array with all schema documents
 */
public static void validate( String xmlDokument, Source[] xsdSchema ) throws SAXException, IOException {   
  SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
  Schema schema = schemaFactory.newSchema( xsdSchema );
  Validator validator = schema.newValidator();
  validator.setErrorHandler( new MyErrorHandler() );
  validator.validate( new StreamSource(new File(xmlDokument)) );
}

Minimal example:

The primary schema:

<xs:schema
    xmlns="baseNamespace"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="baseNamespace"
    xmlns:tns="baseNamespace">

<!-- Define single tag "baseTag" -->
<xs:element name="baseTag">
  <xs:complexType>
    <xs:sequence>
      <xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

The secondary schema:

<xs:schema
    xmlns="secondaryNamespace"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="secondaryNamespace"
    xmlns:tns="secondaryNamespace"
    elementFormDefault="qualified"
    attributeFormDefault="qualified">

<xs:element name="additionalTag"/>

</xs:schema>

The XML document I am trying to validate:

<baseTag
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="baseNamespace"
  xmlns:secondary="secondaryNamespace"
  xsi:schemaLocation="
    baseNamespace base.xsd
    secondaryNamespace secondary.xsd">

  <secondary:additionalTag/>
  <secondary:invalidTag/>
</baseTag>

Using the above Java code giving both schema documents does not produce any validation errors, only if I change the lax to strict in the base schema (which I don't want). The error message in this case is

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'secondary:invalidTag'.

Questions:

Did I misunderstand something and is this actually the correct behavior? Or am I right regarding processContents?

Are my schema documents doing the right thing?

Is my Java code correct? How could I change it so that it behaves as expected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2024-12-17 21:04:20

根据规范:

“它将验证可以获取架构信息的元素和属性,但对于无法获取任何架构信息的元素和属性,它不会发出错误信号。”

因此,当您使用 processContents“lax”时,验证器无法找到“invalidTag”的架构,因此根据规范忽略它。

According to the spec:

"It will validate elements and attributes for which it can obtain schema information, but it will not signal errors for those it cannot obtain any schema information."

So, when you use procesContents "lax", the validator cannot find a schema for the "invalidTag" and therefore ignores it, as per the spec.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文