允许任何内容的 XML 模式 (xsd:any) [英] XML Schema that allows anything (xsd:any)
问题描述
我需要一个 XML 模式的例子,它可以允许任何事情.
I need an example of XML schema that will allow anything and everything.
这听起来可能很奇怪,但我需要它来调试我当前的架构.问题是我有一个复杂的对象,我在一个函数中使用它(我无法控制的 DLL 的一部分)以及一个模式,并且该函数返回给我 XML.现在该函数抛出异常,因为在使用架构进行验证时出现错误,但不应该有.所以,我想要一个空白模式,一个不会导致任何验证错误的模式,所以我可以看到函数输出的 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.
我尝试采用我当前的架构,只保留 xs:schema 标记来创建一个空架构,但这显然不起作用.
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.
推荐答案
XML Schema 无法指定文档无论其内容如何都有效.
但是,如果您能够指定根元素,则可以使用 xs:anyAttribute
和 xs:any
来允许根元素上的任何属性和任何根目录下的 XML:
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:
<?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>
在您的情况下,只要您可以确定有限数量的可能根元素名称,您就可以使用此技术允许具有已知名称的根元素下的任何 XML 内容.
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.
更新:这可以写得更简洁[来源:C.M. Sperberg-McQueen]:
Update: This can be written much more concisely [Credit: 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>
请注意,这允许 但不要求 root
为空.
Note that this is allowing, but not requiring, root
to be empty.
这篇关于允许任何内容的 XML 模式 (xsd:any)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!