开发者

How to enforce one of two types for a single XSD element?

I have an xml element called "PRICE".

I want to allow in this field EITHER a number OR one of the enum values {MAX, MIN开发者_如何学Python, UNAV}. Is this possible with XSD?

This snippet demonstrates what I want, however it is invalid:

       <xs:choice>
           <xs:element name="PRICE">
               <xs:simpleType>
                   <xs:restriction base="xs:string">
                       <xs:enumeration value="MAX"/>
                       <xs:enumeration value="MIN"/>
                       <xs:enumeration value="UNAV"/>
                   </xs:restriction>
               </xs:simpleType>
           </xs:element>
           <xs:element name="PRICE">
               <xs:simpleType>
                   <xs:restriction base="xs:float"/>
               </xs:simpleType>
           </xs:element>
       </xs:choice>


<xs:element name="Price">
    <xs:simpleType>
        <xs:union>
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="MAX"/>
                    <xs:enumeration value="MIN"/>
                    <xs:enumeration value="UNAV"/>
                </xs:restriction>
            </xs:simpleType>
            <xs:simpleType>
                <xs:restriction base="xs:float"/>
            </xs:simpleType>
        </xs:union>
    </xs:simpleType>
</xs:element>


You could try something like this

     <xs:element name="PRICE">
           <xs:simpleType>
              <xs:restriction base="xs:string">
                 <xs:pattern value="MAX|MIN|UNAV|\d+"/>
              </xs:restriction>
           </xs:simpleType>
       </xs:element>

EDIT:

\d+ will work for integers but for floating point numbers, you could use [-+]?[0-9]*\.?[0-9]*

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜