本文介绍了允许任何东西的 XML 模式 (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:anyAttributexs: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.

更新:这可以写得更简洁:

Update: This can be written much more concisely :

<?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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:58