繁体   English   中英

使用XSD将xml元素定义为唯一

[英]Define xml element as unique using xsd

我有一个带有关联的xsd的xml文件:

complexType“ UpgraderConfig”的元素“名称”必须是唯一的。 我在“ UpgraderConfigContainer”定义中添加了“唯一”标记,但似乎无法获得所需的行为。

有人可以告诉我我在做什么错吗?

<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.hp.com/maas/1.0.0/UpgraderConfig"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:config="http://www.hp.com/maas/1.0.0/UpgraderConfig"
           elementFormDefault="qualified"
           version="1.0.0">

    <xs:simpleType name="stringWithoutWhiteSpace">
        <xs:annotation>
            <xs:documentation>One or more occurrences of any character which is not whitespace (i.e. not space, tab, newline, return)</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:minLength value="1" />
            <xs:pattern value="\S+" />
        </xs:restriction>
    </xs:simpleType>


    <xs:complexType name="UpgraderConfigContainer">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="Upgrader" type="config:UpgraderConfig" >
                <xs:unique name="uniqueUgraderName">
                    <xs:selector xpath="Upgrader"/>
                    <xs:field xpath="name"/>
                </xs:unique>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="UpgraderConfig">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="className" type="config:stringWithoutWhiteSpace"/>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="UpgraderConfigs" type="config:UpgraderConfigContainer" />

</xs:schema>

您定义的唯一约束太低。 元素本身不能唯一。 只有包含它的容器才能具有唯一约束。 您已将UpgraderConfigContainer标记为有点UpgraderConfigContainer ,因为它不是容器。 相反,它是未绑定的元素,这意味着您具有零个或多个UpgraderConfigContainer 包含UpgraderConfigContainer的元素必须具有唯一的约束。 您必须将其添加到UpgraderConfigs

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:config="http://www.hp.com/maas/1.0.0/UpgraderConfig" targetNamespace="http://www.hp.com/maas/1.0.0/UpgraderConfig" elementFormDefault="qualified" version="1.0.0">
    <xs:simpleType name="stringWithoutWhiteSpace">
        <xs:annotation>
            <xs:documentation>One or more occurrences of any character which is not whitespace (i.e. not space, tab, newline, return)</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:pattern value="\S+"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="UpgraderConfigContainer">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="Upgrader" type="config:UpgraderConfig"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="UpgraderConfig">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="className" type="config:stringWithoutWhiteSpace"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="UpgraderConfigs" type="config:UpgraderConfigContainer">
        <xs:unique name="uniqueUpgraderName">
            <xs:selector xpath="config:Upgrader"/>
            <xs:field xpath="config:name"/>
        </xs:unique>
    </xs:element>
</xs:schema>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM