Define new attribute to elements XSD

会有一股神秘感。 提交于 2020-01-05 11:02:43

问题


I have a question regarding defining new attribute for my elements in my xml schema (xsd) I have an element like this

<xs:element name="xyz" type="xs:hexBinary" minOccurs="2">

I want to add a new attribute "size" where I can specify the size of the element "xyz". How can I define do this?

Thanks for your help


回答1:


Below is a simple valid XSD, built around your xyz element.

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:complexType name="dummy">
        <xs:sequence>
            <xs:element name="xyz" minOccurs="2">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:hexBinary">
                            <xs:attribute name="size" type="xs:int" use="required"/>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Any element that has attributes and/or nested elements MUST be of complex type. If there are nested elements, then the content model of the complex type must be complexContent; otherwise, it must be simpleContent.

This approach doesn't allow you to also constrain the base type (here hexBinary). If you need to also specify constraining facets for the simple base type, then you have to separately create a new simple type, a restriction of hexBinary, and then extend the new type with attributes.



来源:https://stackoverflow.com/questions/23077279/define-new-attribute-to-elements-xsd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!