开发者

Can XSD define a wildcard complex type?

Say I don't know what an element's name will be, but I do know what it's children will be. For example, the names "foo" and "bar" are not prescribed but "A", "B" & "C" are.

<example>
    <foo>
        <A>A</A>
        <B>B</B>
        <C>C</C>
    </foo>
    <bar>
        <A>A</A>
        <B>B</B>
        <C>C</C>
    </bar>
</example>

I cannot leave the name attribute out because that's a violation. I would expect to be able to do this instead:

<xs:element name="example">
    <xs:complexType>
        <xs:sequence maxOccurs="unbounded">
            <xs:any>
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="A" type="xs:string"/>
                        <xs:element name="B" type="xs:string"/>
       开发者_JAVA百科                 <xs:element name="C" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:any>
        </xs:sequence>
    </xs:complexType>
</xs:element>

This does not work either, <xs:any> can only contain annotations and refuses a type.

Is there something I can do with namespaces that will work with unknown element names? Should I give up, not attempt to validate the children and just document what the contents must be?


You could try doing this with substitution groups:

<xs:element name="example">
  <xs:sequence>
    <xs:element ref="ABCSequence" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
</xs:element>

<xs:element name="ABCSequence" abstract="true" type="ABCSeq" />
<xs:complexType name="ABCSeq">
  <xs:complexContent>
    <xs:sequence>
      <xs:element name="A" type="xsd:string" />
      <xs:element name="B" type="xsd:string" />
      <xs:element name="C" type="xsd:string" />
    </xs:sequence>
  </xs:complexContent>
</xs:complexType>

<xs:element name="foo" substitutionGroup="ABCSequence" type="ABCSeq" />
<xs:element name="bar" substitutionGroup="ABCSequence" type="ABCSeq" />

I'm not sure if that will allow arbitrary external elements to be added in without declaring their types (via xsi:type attributes) but it does at least allow describing the sort of document you're after.


You cannot quite achieve what you want to do there in XML Schema. There is a close solution, but not quite what you want:

<xs:element name="example">
    <xs:complexType>
        <xs:sequence maxOccurs="unbounded">
            <xs:any namespace="##other" processContents="lax"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

Now you could provide separate schemas for the elements that can occur in there, e.g. one for foo, under a separate namespace and in a separate schema file:

<xs:element name="foo">
    <xs:complexType>
        ....
    </xs:complexType>
</xs:element>

That's about all you can do (it's your "multiple namespaces" solution). You can't avoid listing the elements entirely. If that's not good enough, then <xsd:any> with processContents set to skip is your only solution, followed by external validation (code, Schematron, etc)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜