-->

Restrict the a XML request attribute value and few

2019-07-31 23:29发布

问题:

This post is an extension of the post :Restrict the attribute value and the value of the element using XSD

Now I am able to restrict all the attribute & element value using the XSD. But I'm now looking to restrict only one or two of the element value and not all of them.

XML:

<response src="XML">
  <resp name="JSON">letter.c</resp>
  <resp name="SWAGGER">di.js</resp>
  <resp name="BI">bi.j</resp>
</response>

XSD:

<xs:schema attributeFormDefault="unqualified"
          elementFormDefault="qualified"
          xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="response">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="resp" maxOccurs="14" minOccurs="1">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="respFilter">
                                <xs:attribute name="name" use="required" 
                                              type="Enum" />
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="src" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="XML"></xs:enumeration>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="Enum">
        <xs:restriction base="xs:string">
            <xs:enumeration value="JSON">
            </xs:enumeration>
            <xs:enumeration value="SWAGGER">
            </xs:enumeration>
            <xs:enumeration value="BI">
            </xs:enumeration>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="respFilter">
        <xs:restriction base="xs:string">
            <xs:minLength value="0"></xs:minLength>
            <xs:maxLength value="1064"></xs:maxLength>
            <xs:enumeration value="letter.c"/>
            <xs:enumeration value="di.js"/>
            <xs:enumeration value="bi.j"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

This XSD allows only the above XML request if it has the below value in it:

letter.c
di.js
bi.j

But what I'm looking to do now is to dynamically allow few element values so that the XSD validates all the below requests.

1.

<response src="XML">
  <resp name="JSON">letter.c</resp>
  <resp name="SWAGGER"></resp>
  <resp name="BI">bi.j</resp>
</response>

2.

<response src="XML">
  <resp name="JSON">letter.c</resp>
  <resp name="SWAGGER">23.x</resp>
  <resp name="BI">bi.j</resp>
</response>

3.

<response src="XML">
  <resp name="JSON">letter.c</resp>
  <resp name="SWAGGER">di.js</resp>
  <resp name="BI">dj.c</resp>
</response>

So basically, attribute JSON will always have letter.c, anything other than letter.c in JSON should be discarded by the XSD. On the other hand attribute SWAGGER & BI can have any or empty value in it.

回答1:

In order to have validation vary per the value of @name, you need assertions or conditional type assignment from XSD 1.1. If you're limited to XSD 1.0, you'll have to test your constraint out-of-band with respect to XSD, or you'll have to redesign your XML to make it less meta:

<response src="XML">
  <JSON>letter.c</JSON>
  <SWAGGER>di.js</SWAGGER>
  <BI>bi.j</BI>
</response>

This XML could use a fixed value for JSON and enumerations for SWAGGER and BI in XSD 1.0.