简体   繁体   English

通过XSD在XML中的唯一元素值

[英]Unique element value in xml via xsd

I have a xsd which has to prevent duplicate values for an element. 我有一个xsd,它必须防止元素重复的值。 I tried various ways and was somehow missing to achieve unique constraint for an element. 我尝试了各种方法,但以某种方式缺少实现元素的唯一约束的方法。

In the below xml, i have xyz:interval element having duplicate values. 在下面的xml中,我有具有重复值的xyz:interval元素。 How to avoid the duplication using unique tag? 如何避免使用唯一标签重复?

I have tried using unique in below XSD, but was not able to achieve the same. 我曾尝试在XSD下使用unique,但无法达到相同目的。

XSD: XSD:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xyz="http://www.example.com/schema/public/" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.example.com/schema/public/" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Intervals">
    <xs:sequence>
        <xs:element name="interval" type="xs:int" maxOccurs="unbounded">
            <xs:unique name="alias_unique">
                <xs:selector xpath="supportedMeasurementIntervals" />
                <xs:field xpath="." />
            </xs:unique>
        </xs:element>
        <xs:element name="defaultInterval" type="xs:int" minOccurs="1" maxOccurs="1" />
    </xs:sequence>
</xs:complexType>
<xs:element name="NoOfPeriod" type="xs:int" />
<xs:element name="isPeriodSupported" type="xs:boolean" />
<xs:element name="MType">
    <xs:complexType mixed="true">
        <xs:sequence minOccurs="0">
            <xs:element name="SIntervals" type="xyz:Intervals" minOccurs="0" maxOccurs="1" />
        </xs:sequence>
        <xs:attribute name="id" type="xs:string" use="required" />
    </xs:complexType>
</xs:element>
<xs:group name="Child">
    <xs:sequence>
        <xs:element name="SIntervals" type="xyz:Intervals" minOccurs="1" maxOccurs="1" />
        <xs:element ref="xyz:NoOfPeriod" minOccurs="1" maxOccurs="1" />
        <xs:element ref="xyz:isPeriodSupported" minOccurs="1" maxOccurs="1" />
    </xs:sequence>
</xs:group>
<xs:element name="Parent">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Child" minOccurs="1" maxOccurs="1">
                <xs:complexType>
                    <xs:group ref="xyz:Child" />
                </xs:complexType>
            </xs:element>
            <xs:element name="MTypes" minOccurs="1" maxOccurs="1">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="xyz:MType" maxOccurs="unbounded" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

XML : XML

<?xml version="1.0" encoding="utf-8"?>
<xyz:Parent xmlns:xyz="http://www.example.com/schema/public/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/schema/public/">
    <xyz:Child>
        <xyz:SIntervals>
            <xyz:interval>1111</xyz:interval>
            <xyz:interval>1111</xyz:interval>
            <xyz:interval>1111</xyz:interval>
            <xyz:interval>9727</xyz:interval>
            <xyz:defaultInterval>504</xyz:defaultInterval>
        </xyz:SIntervals>
        <xyz:NoOfPeriod>4804</xyz:NoOfPeriod>
        <xyz:isPeriodSupported>1</xyz:isPeriodSupported>
    </xyz:Child>
    <xyz:MTypes>
        <xyz:MType id="string" />
        <xyz:MType id="string" />
        <xyz:MType id="string">
            <xyz:SIntervals>
                <xyz:interval>2222</xyz:interval>
                <xyz:interval>2222</xyz:interval>
                <xyz:defaultInterval>6631</xyz:defaultInterval>
            </xyz:SIntervals>
        </xyz:MType>
        <xyz:MType id="string" />
    </xyz:MTypes>
</xyz:Parent>

Actually, you could change the maxOccurs="1" instead of "unbounded" in your definition of Intervals . 实际上,您可以在Intervals的定义中更改maxOccurs =“ 1”而不是“ unbounded”。

    [.....]
    <xs:complexType name="Intervals">
        <xs:sequence>
              <xs:element name="interval" type="xs:int" maxOccurs="1">
    [.....]

EDIT (After the comment) 编辑(评论后)

  1. you have to put your defined the namespace in the xpath expression. 您必须将定义的名称空间放入xpath表达式中。
  2. as said in the comment, the unique apply to the scope of the element . 如评论中所述, unique适用于element的范围。 you defined it a level too low. 您将其定义为太低的水平。

for example, you could define the uniqueness in the scope of the Child element like following: 例如,您可以在Child元素的范围内定义唯一性,如下所示:

<xs:group name="Child">
<xs:sequence>
    <xs:element name="SIntervals" type="xyz:Intervals" minOccurs="1" maxOccurs="1" >
        <xs:unique name="uniqueInterval">
            <xs:selector xpath="xyz:interval" />
            <xs:field xpath="." />
        </xs:unique>
    </xs:element>
    <xs:element ref="xyz:NoOfPeriod" minOccurs="1" maxOccurs="1" />
    <xs:element ref="xyz:isPeriodSupported" minOccurs="1" maxOccurs="1" />
</xs:sequence>

You'll then have to do it the same way for your other elements (or you re-design the definition). 然后,您必须对其他元素执行相同的操作(或重新设计定义)。

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

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