开发者

XML Schema, one fixed attribute amongst several unknown

开发者 https://www.devze.com 2023-02-12 20:33 出处:网络
I have a schema that I use to validate HTTP requests to my app. It works really well for the query string and post content, but I\'ve hit a stumbling block with the header parameters. Ideally I\'d lik

I have a schema that I use to validate HTTP requests to my app. It works really well for the query string and post content, but I've hit a stumbling block with the header parameters. Ideally I'd like to chec开发者_JAVA技巧k that HTTP_REQUEST_METHOD is 'GET' or 'POST' etc, but other than that, I don't care what values the other parameters have.

So, my XML might look like:

<REQUEST>
 <HEADERS>
  <User-Agent>bla bla</User-Agent>
  <Cookie>bla bla</Cookie>

   ...

  <request_method>GET</request_method>

   ...

  <remote_port>bla bla</remote_port>
 </HEADERS>
 <QUERY_STRING>
   ...
 </QUERY_STRING>
</REQUEST>

Is there any means by which I can specify a wildcard for the header parameters that I can't forsee, whilst insisting that if there's a tag that its contents are fixed?

Sort of this:

<xsd:any namespace="##any" minOccurs="0" processContents="lax"/>
... fixed value of 'GET' for <request_method> ...
<xsd:any namespace="##any" minOccurs="0" processContents="lax"/>


That's basically what lax is for. It will validate elements it knows about and ignore others. You would just put a single any in you content model with maxOccurs=unbounded and also define the known parameters like request_header as global elements.

The only catch is that any other defined global element will also be validated if it occurs there.

Example schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="HEADERS">
    <xs:complexType>
      <xs:sequence>
        <xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="request_method">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value="GET"/>
        <xs:enumeration value="POST"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
</xs:schema>

There are richer options in XML Schema 1.1 in case your validator has support for it.

0

精彩评论

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