开发者

Restrict values of an element to be unique within the elements under the same XPath

开发者 https://www.devze.com 2022-12-21 02:21 出处:网络
Consider this XML - <Root> <Foo> <UniqueValue>A100</UniqueValue> </Foo> <Foo>

Consider this XML -

<Root>
  <Foo>
    <UniqueValue>A100</UniqueValue>
  </Foo>
  <Foo>
    <UniqueValue>A101</UniqueValue>
  </Foo>
  <Foo>
    <UniqueValue>B102</UniqueValue>
  </Foo>
  <Foo>
    <UniqueValue>A101</UniqueValue> <!-- This should be an error -->
  </Foo>
  <开发者_开发技巧Foo>
    <UniqueValue> A101 </UniqueValue> <!-- This should be an error but not critical for now -->
  </Foo>
</Root>

How do I restrict the simple type element <UniqueValue> such that its values are unique? Also not that I don't want to restrict to numbers.


Try putting a uniqueness constraint on "Root". Note that this needs to be ont he element definition, not a type definition:

<xsd:element name="Root" type="RootType">
  <xsd:unique name="uniqueValues">
    <xsd:selector xpath="Foo"/>
    <xsd:field xpath="UniqueValue"/>
  </xsd:unique>
</xsd:element>

Depending on the parser you are using, you may have to switch on additional options to get this validated (e.g. "full schema validation", identity constraint validation, etc).

0

精彩评论

暂无评论...
验证码 换一张
取 消