XML Schema that allows anything (xsd:any)

前端 未结 3 952
南旧
南旧 2020-11-30 13:39

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 tha

相关标签:
3条回答
  • 2020-11-30 14:32

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

    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.

    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.

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

    0 讨论(0)
  • 2020-11-30 14:35

    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.

    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.

    Caveat: I've considerably simplified the rules here.

    0 讨论(0)
  • 2020-11-30 14:37

    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.

    0 讨论(0)
提交回复
热议问题