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
706 views
in Technique[技术] by (71.8m points)

xml - How can I define an XSD file that allows unknown (wildcard) elements?

I'm receiving an XML message with unknown variable name elements... that is, they are not predefined...

I only know there can be 0 or more of those elements, along with some other that are mandatory...

For example

<root>
    <service>my service</service>
    <resource>my resource</resource>
    <action>update</action>
    <parameters>
      <field1>value1</field1>
      <field2>value2</field2>
      <field3>value3</field3>
    </parameters>
</root>

that is, I don't know what will be passed as "parameters", I only know there will be 0 or more elements with a value, no deeper tag nesting allowed....

I was thinking about something like

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="root">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="service" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/>
      <xs:element name="resource" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/>
      <xs:element name="action" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/>
      <xs:element name="parameters">
        <xs:complexType>
          <xs:element name="*" maxOccurs="unbounded">
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

of course, the hard part is

<xs:element name="*" maxOccurs="unbounded">

Is it possible to do such a thing?
How can I define an XSD file that validates such a message?

--

I checked the w3c reference at

http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/datatypes.html#NCName

and it says:

The ·lexical space· of NCName is the set of all strings which ·match· the NCName production of [Namespaces in XML].

So what does it mean?
besides... could you recommend me some easy way to test compliance with an XSD definition?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

What you want is a wildcard particle, for details see http://www.w3.org/TR/xmlschema-1/#Wildcards

To do it you can use xs:any. Note that xs:element and xs:any cannot be placed directly inside an xs:complexType. You need a container like a xs:sequence or xs:choice.

A valid schema that handles wildcards is below:

<?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:element name="service" type="xs:string"/>
        <xs:element name="resource" type="xs:string"/>
        <xs:element name="action" type="xs:string"/>
        <xs:element name="parameters">
          <xs:complexType>
            <xs:sequence maxOccurs="unbounded">
              <xs:any processContents="lax"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

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

...