允许任何内容的XML Schema(xsd:any)

时间:2021-03-13 17:19:49

I need an example of XML schema that will allow anything and everything.

我需要一个XML模式的例子,它将允许任何事物和一切。

It might sound weird like this, but I need that to debug my current schema. The thing is that I have a complex object that I use in a function (part of a DLL I have no control over) along with a schema, and that functions returns me the XML. For now the function throws an exception because there's an error while validating with the schema, but there shouldn't be one. So, I want a blank schema, a schema that will not cause any validation error, so I can see the XML outputted by the function.

这可能听起来很奇怪,但我需要调试我当前的架构。问题是我有一个复杂的对象,我在一个函数(我无法控制的DLL的一部分)和模式中使用,并且该函数返回XML。现在,该函数抛出异常,因为在使用模式进行验证时出现错误,但不应该有一个。所以,我想要一个空白模式,一个不会导致任何验证错误的模式,所以我可以看到该函数输出的XML。

I tried to take my current schema, and keep only the xs:schema tag to create an empty schema, but that obviously didn't work.

我试图采用我当前的架构,并只保留xs:schema标签来创建一个空架构,但这显然不起作用。

3 个解决方案

#1


9  

XML Schema cannot specify that a document is valid regardless of its content.

无论文档的内容如何,​​XML Schema都无法指定文档是否有效。

However, if you're able to specify the root element, you can use xs:anyAttribute and xs:any to allow any attributes on the root element and any XML under the root:

但是,如果您能够指定根元素,则可以使用xs:anyAttribute和xs:any来允许根元素上的任何属性以及根目录下的任何XML:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:any processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:anyAttribute processContents="skip"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

In your case, as long as you can be assured of a finite number of possible root element names, you can use this technique to allow any XML content under a root element with a known name.

在您的情况下,只要您可以确保有限数量的可能根元素名称,您就可以使用此技术来允许具有已知名称的根元素下的任何XML内容。


Update: This can be written much more concisely [Credit: C. M. Sperberg-McQueen]:

更新:这可以写得更简洁[信用:C. M. Sperberg-McQueen]:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root"/>
</xs:schema>

Note that this is allowing, but not requiring, root to be empty.

请注意,这允许但不要求root为空。

#2


2  

It's often assumed that an XML schema partitions documents into those that are valid and those that are not. It's actually rather more subtle than that. When you invoke validation, you need to say how you want the validation done. The most common invocation is strict validation, in which case either the name of the root element in your instance document must correspond to the name of a global element declaration in your schema, or it must have an xsi:type attribute that matches a global type definition in your schema. It follows that no finite schema will match every document instance under strict validation.

通常假设XML模式将文档分为有效文档和非有效文档。它实际上比那更微妙。当您调用验证时,您需要说明您希望如何完成验证。最常见的调用是严格验证,在这种情况下,实例文档中根元素的名称必须与模式中全局元素声明的名称相对应,或者它必须具有与全局类型匹配的xsi:type属性架构中的定义。因此,在严格验证下,没有有限的模式匹配每个文档实例。

In principle you can also invoke a schema processor to do lax validation. In this case, if there is no match for the root element name among the global element declarations in the schema, validation succeeds. So the empty schema (no declarations) matches every instance document under lax validation.

原则上,您还可以调用模式处理器来执行宽松验证。在这种情况下,如果架构中的全局元素声明中的根元素名称不匹配,则验证成功。因此,空模式(无声明)与松散验证下的每个实例文档相匹配。

You can also invoke validation against a named type. If you invoke validation against the named type xs:anyType, then every instance is valid, regardless what the schema says.

您还可以针对命名类型调用验证。如果针对命名类型xs:anyType调用验证,则无论架构如何说明,每个实例都是有效的。

Caveat: I've considerably simplified the rules here.

警告:我在这里大大简化了规则。

#3


0  

You don't need or want a schema. XML documents can be classified as "well-formed" and "valid".

您不需要或不需要架构。 XML文档可以被分类为“格式良好”和“有效”。

As the above answer states:

如上所述,答案如下:

XML that adheres to the XML standard is considered well formed, while XML that adheres to a DTD [or schema] is considered valid.

遵循XML标准的XML被认为是良好的形式,而遵守DTD [或模式]的XML被认为是有效的。

I should point out that all valid XML documents are also well-formed. But in your case, you don't care about the 'validity' of the XML document, just that it is well-formed. All XML parsers can (indeed must) check for well-formedness, and doing schema validation usually requires additional steps.

我应该指出,所有有效的XML文档也都是格式良好的。但在您的情况下,您并不关心XML文档的“有效性”,只是它的格式正确。所有XML解析器都可以(实际上必须)检查格式良好,并且进行模式验证通常需要额外的步骤。

Look into how you can do the former (check for well-formedness) without forcing the latter (validation).

看看你如何能够做到前者(检查结构良好)而不强迫后者(验证)。

#1


9  

XML Schema cannot specify that a document is valid regardless of its content.

无论文档的内容如何,​​XML Schema都无法指定文档是否有效。

However, if you're able to specify the root element, you can use xs:anyAttribute and xs:any to allow any attributes on the root element and any XML under the root:

但是,如果您能够指定根元素,则可以使用xs:anyAttribute和xs:any来允许根元素上的任何属性以及根目录下的任何XML:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:any processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:anyAttribute processContents="skip"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

In your case, as long as you can be assured of a finite number of possible root element names, you can use this technique to allow any XML content under a root element with a known name.

在您的情况下,只要您可以确保有限数量的可能根元素名称,您就可以使用此技术来允许具有已知名称的根元素下的任何XML内容。


Update: This can be written much more concisely [Credit: C. M. Sperberg-McQueen]:

更新:这可以写得更简洁[信用:C. M. Sperberg-McQueen]:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root"/>
</xs:schema>

Note that this is allowing, but not requiring, root to be empty.

请注意,这允许但不要求root为空。

#2


2  

It's often assumed that an XML schema partitions documents into those that are valid and those that are not. It's actually rather more subtle than that. When you invoke validation, you need to say how you want the validation done. The most common invocation is strict validation, in which case either the name of the root element in your instance document must correspond to the name of a global element declaration in your schema, or it must have an xsi:type attribute that matches a global type definition in your schema. It follows that no finite schema will match every document instance under strict validation.

通常假设XML模式将文档分为有效文档和非有效文档。它实际上比那更微妙。当您调用验证时,您需要说明您希望如何完成验证。最常见的调用是严格验证,在这种情况下,实例文档中根元素的名称必须与模式中全局元素声明的名称相对应,或者它必须具有与全局类型匹配的xsi:type属性架构中的定义。因此,在严格验证下,没有有限的模式匹配每个文档实例。

In principle you can also invoke a schema processor to do lax validation. In this case, if there is no match for the root element name among the global element declarations in the schema, validation succeeds. So the empty schema (no declarations) matches every instance document under lax validation.

原则上,您还可以调用模式处理器来执行宽松验证。在这种情况下,如果架构中的全局元素声明中的根元素名称不匹配,则验证成功。因此,空模式(无声明)与松散验证下的每个实例文档相匹配。

You can also invoke validation against a named type. If you invoke validation against the named type xs:anyType, then every instance is valid, regardless what the schema says.

您还可以针对命名类型调用验证。如果针对命名类型xs:anyType调用验证,则无论架构如何说明,每个实例都是有效的。

Caveat: I've considerably simplified the rules here.

警告:我在这里大大简化了规则。

#3


0  

You don't need or want a schema. XML documents can be classified as "well-formed" and "valid".

您不需要或不需要架构。 XML文档可以被分类为“格式良好”和“有效”。

As the above answer states:

如上所述,答案如下:

XML that adheres to the XML standard is considered well formed, while XML that adheres to a DTD [or schema] is considered valid.

遵循XML标准的XML被认为是良好的形式,而遵守DTD [或模式]的XML被认为是有效的。

I should point out that all valid XML documents are also well-formed. But in your case, you don't care about the 'validity' of the XML document, just that it is well-formed. All XML parsers can (indeed must) check for well-formedness, and doing schema validation usually requires additional steps.

我应该指出,所有有效的XML文档也都是格式良好的。但在您的情况下,您并不关心XML文档的“有效性”,只是它的格式正确。所有XML解析器都可以(实际上必须)检查格式良好,并且进行模式验证通常需要额外的步骤。

Look into how you can do the former (check for well-formedness) without forcing the latter (validation).

看看你如何能够做到前者(检查结构良好)而不强迫后者(验证)。