开发者

Can I make text of an element (complexType and mixed) mandatory through XSD and one attribute mandatory based on enumeration

开发者 https://www.devze.com 2023-02-17 13:23 出处:网络
I have this XML <button onclick=\"alert(\'submit\')\" replace=\"append\" forid=\"loginbutton\" id=\"btnLogin\" >Click Me<开发者_Python百科;/button>

I have this XML

<button onclick="alert('submit')" replace="append" forid="loginbutton" id="btnLogin" >Click Me<开发者_Python百科;/button>

And I have this XSD

  <xs:element name="button" >
    <xs:complexType mixed="true">
                <xs:attribute name="forid" use="required" type="xs:string" />
                <xs:attribute name="onclick" use="required" />
                <xs:attribute name="id" use="optional" />
                <xs:attribute name="replace" use="required">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="modify" />
                            <xs:enumeration value="append" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
    </xs:complexType>
  </xs:element>

I want two things here

  1. The button content text should be mandatory in all cases
  2. If the value of replace attribute is 'append' then attribute id will be mandatory


If you make this a complex-type-with-simple-content, then you can constrain the permitted values of the text using xs:enumeration. If you make it a complex-type-with-mixed-content, then you can't constrain what goes in the text. It seems to me you want simple content here, not mixed content (there are no child elements).

You can't define a co-constraint (values of one thing dependent on the values of another) using XSD 1.0 - for that you need XSD 1.1. Support for XSD 1.1 is currently available only in Saxon and Xerces.


I managed to get it done somehow. I quite did not understand it, basically I just did trial and error. If someone can explain what this means I will be grateful.

<xs:element name="button">
    <xs:complexType>
        <xs:simpleContent>
            <xs:restriction base="xs:anyType">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:minLength value="1" />

                    </xs:restriction>
                </xs:simpleType>
                <xs:attribute name="forid" use="required" type="xs:string" />
                <xs:attribute name="onclick" />
                <xs:attribute name="id" use="optional" />
                <xs:attribute name="type" use="required" />
                <xs:attribute name="replace" use="required">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="modify" />
                            <xs:enumeration value="append" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>

            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>
0

精彩评论

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