为元素 XSD 定义新属性 [英] Define new attribute to elements XSD

查看:23
本文介绍了为元素 XSD 定义新属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在我的 xml 架构 (xsd) 中为我的元素定义新属性的问题我有一个这样的元素

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">

我想添加一个新属性size",我可以在其中指定元素xyz"的大小.我如何定义这样做?

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

感谢您的帮助

推荐答案

下面是一个简单有效的 XSD,围绕您的 xyz 元素构建.

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>

任何具有属性和/或嵌套元素的元素都必须是复杂类型.如果有嵌套元素,那么复杂类型的内容模型必须是complexContent;否则,它必须是 simpleContent.

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.

这种方法不允许你同时约束基本类型(这里是 hexBinary).如果您还需要为简单基类型指定约束方面,那么您必须单独创建一个新的简单类型,限制hexBinary,然后用属性扩展新类型.

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.

这篇关于为元素 XSD 定义新属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆