开发者

XSD - restrict/redefine complexType with occurs 0

开发者 https://www.devze.com 2023-02-16 21:13 出处:网络
I have a complexType User: <xs:complexType name=\"User\"> <xs:all> <xs:element name=\"id\" type=\"xs:positiveInteger\" minOccurs=\"1\" maxOccurs=\"1\" />

I have a complexType User:

<xs:complexType name="User">
    <xs:all>
        <xs:element name="id" type="xs:positiveInteger" minOccurs="1" maxOccurs="1" />
        <xs:element name="email" type="email" minOccurs="1" maxOccurs="1" />
        <xs:element name="password" type="xs:string" minOccurs="1" maxOc开发者_运维技巧curs="1" />
        <xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1" />
    </xs:all>
</xs:complexType>

and I have to use it with constraints on elements minOccurs and maxOccurs.

For example by login I need the User with email and password elements only, so I need any other elements (id,name) with maxOccurs=0 and minOccurs=0.

<Auth:login>
    <User>
        <email>my@email.net</email>
        <password>mypass</password>
    </User>
</Auth:login>

Is it possible to validate the XML above with a restricted complexType of User?


No: type R can only restrict type B if all instances of R are valid instances of B. Since B requires id and name to be present, a type that requires them (or even permits them) to be absent is not a valid restriction of B.


It's possible with assertions (in xsd 1.1), but I think php has only xsd 1.0 support :S

I found here some help. I can put my sub elements into groups, and concatenate the actually needed groups.

0

精彩评论

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

关注公众号