Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

XML Schema that allows anything (xsd:any)

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

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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>

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 [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>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...