在这种情况下如何添加唯一约束?

how to add unique constraint in this case?

我有以下 XSD 片段:

<xs:complexType name="JsonLayout">
    <xs:complexContent>
        <xs:extension base="ns:LayoutTypeBase">
            <xs:sequence>
                <xs:element name="attribute" minOccurs="1" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:attribute name="name" use="required" type="ns:nonEmptyString" />
                        <xs:attribute name="layout" use="required" type="ns:nonEmptyString" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

这扩展了只是空的 LayoutTypeBase:

<xs:complexType name="LayoutTypeBase" />

在 JsonLayout 中,我想添加一个唯一约束以确保它们不会指定 2 个具有相同名称的属性。

除内部元素外,VS 不允许我在此处的任何地方添加唯一约束。但这不会击中 "collection",它会击中元素本身,对吗?我需要它在 "collection".

我尝试将 complexType 包装在一个元素中并将名称转移到该元素,然后我能够添加一个唯一约束,但是当我有一个欺骗名称时它没有触发错误:

<xs:element name="JsonLayout">
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="ns:LayoutTypeBase">
                <xs:sequence>
                    <xs:element name="attribute" minOccurs="1" maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:attribute name="name" use="required" type="ns:nonEmptyString" />
                            <xs:attribute name="layout" use="required" type="ns:nonEmptyString" />
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:unique name="jsonLayout.uniqueAttributeName">
        <xs:selector xpath="attribute" />
        <xs:field xpath="@name" />
    </xs:unique>
</xs:element>

此处正确的 xpath 语法是什么?还是我做错了?

谢谢!

你的问题一定是你的架构有一个 targetNamespace。因此,您必须为该命名空间定义一个前缀(例如 tns),并将该前缀与您的选择器一起使用(tns:attribute)。

为了完整起见,在您的场景中您也可以使用 xs:key(而不是唯一的),因为您的选择器和字段指向必填内容。

如果您想问为什么需要别名,请仔细阅读 SO post 更新部分(显示带有命名空间的语法)。

根据您的评论,这可能符合您的情况:

XSD:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns:tns="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xtm="http://paschidev.com/schemas/metadata/xtm">
    <xs:complexType name="JsonLayout">
        <xs:complexContent>
            <xs:extension base="LayoutTypeBase">
                <xs:sequence>
                    <xs:element name="attribute" minOccurs="1" maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:attribute name="name" use="required" type="xs:string"/>
                            <xs:attribute name="layout" use="required" type="xs:string"/>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="LayoutTypeBase"/>
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
        <xs:key name="k">
            <xs:selector xpath="*/tns:attribute"/>
            <xs:field xpath="@name"/>
        </xs:key>
    </xs:element>
</xs:schema>

示例(无效)XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xtm="http://paschidev.com/schemas/metadata/xtm" xmlns="http://tempuri.org/XMLSchema.xsd">
    <some xsi:type="JsonLayout">
        <attribute name="a" layout="b"/>
        <attribute name="a" layout="b"/>
    </some>
</root>