我有以下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并没有让我在这里添加一个独特的约束。但那不会打击&#34;集合&#34;,它会打击元素本身吗?我需要它在&#34;集合&#34;。我尝试将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语法是什么?或者我做错了吗?
谢谢!
答案 0 :(得分:1)
Your problem must be that your schema has a targetNamespace. Because of this, you must define a prefix for that namespace (e.g. tns), and use that prefix with your selector (tns:attribute).
Just for completeness, in your scenario you could use xs:key as well (instead of unique) since your selector and field point to mandatory content.
If you think about asking why you need an alias, please go through this SO post, the UPDATE section (showing syntax with namespaces).
Based on your comment, this may match your scenario:
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>
Sample (invalid) 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>