xsd:具有可选属性的唯一

发布于 2024-10-05 13:14:39 字数 845 浏览 10 评论 0原文

我有这个 Xml 文件:

<objects>
  <object name="ID1" />
  <object name="ID2" />
  <object name="ID2" color="green" />
  <object name="ID3" color="green" />
<objects>

我想根据 XSD 架构验证该文件,以便 namecolor 之间的组合在文档中是唯一的。

问题是,如果我使用:

<xs:unique name="UniqueObjectNameColor">
  <xs:selector xpath="./object" />
  <xs:field xpath="@name" />
  <xs:field xpath="@color" />
</xs:unique>

... 规则将忽略没有可选 color 属性的 object 元素。以下内容验证正确,但不应该验证。

  <object name="ID2" />
  <object name="ID2" />

您能否告诉我,当元素中不存在 color 属性时,如何指定强制执行唯一 namecolor 组合的规则object,它检查name

I have this Xml file:

<objects>
  <object name="ID1" />
  <object name="ID2" />
  <object name="ID2" color="green" />
  <object name="ID3" color="green" />
<objects>

I would like to validate this against an XSD Schema, so that the combination between name and color are unique in the document.

The problem is that, if I use:

<xs:unique name="UniqueObjectNameColor">
  <xs:selector xpath="./object" />
  <xs:field xpath="@name" />
  <xs:field xpath="@color" />
</xs:unique>

... the rule will ignore object elements without the optional color attribute. The following validates correctly while it shouldn't.

  <object name="ID2" />
  <object name="ID2" />

Can you tell me how can I specify a rule that enforces unique name and color combinations and, when the color attribute is not present in the element object, it checks the name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人迟暮 2024-10-12 13:14:39

使用 usedefault 带或不带如下值:

    <element name="objects">
        <complexType>
            <sequence>
                <element name="object" maxOccurs="unbounded">
                    <complexType>
                        <attribute name="name" type="string" />
                        <attribute name="color" type="string" use="optional" default="noColor" />
                    </complexType>
                </element>
            </sequence>
        </complexType>
        <unique name="UniqueObjectNameColor">
            <selector xpath="tns:object" />
            <field xpath="@name" />
            <field xpath="@color" />
        </unique>
    </element>

</schema>

Use use and default with or without a value like:

    <element name="objects">
        <complexType>
            <sequence>
                <element name="object" maxOccurs="unbounded">
                    <complexType>
                        <attribute name="name" type="string" />
                        <attribute name="color" type="string" use="optional" default="noColor" />
                    </complexType>
                </element>
            </sequence>
        </complexType>
        <unique name="UniqueObjectNameColor">
            <selector xpath="tns:object" />
            <field xpath="@name" />
            <field xpath="@color" />
        </unique>
    </element>

</schema>
随心而道 2024-10-12 13:14:39

老问题,但值得回答。每个元素可以使用多个唯一约束。这会做你想做的事:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/xsdunique-with-optional-properties"
    xmlns:tns="http://www.example.org/xsdunique-with-optional-properties"
    elementFormDefault="qualified" attributeFormDefault="unqualified">

    <element name="objects">
        <complexType>
            <sequence>
                <element name="object" maxOccurs="unbounded">
                    <complexType>
                        <attribute name="name" type="string" />
                        <attribute name="color" type="string" />
                    </complexType>
                </element>
            </sequence>
        </complexType>
        <unique name="UniqueObjectName">
            <selector xpath="tns:object" />
            <field xpath="@name" />
        </unique>
        <unique name="UniqueObjectNameColor">
            <selector xpath="tns:object" />
            <field xpath="@name" />
            <field xpath="@color" />
        </unique>
    </element>

</schema>

Old question, but worth to be answered. You can use more than one unique constraint per element. This would do what you want:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/xsdunique-with-optional-properties"
    xmlns:tns="http://www.example.org/xsdunique-with-optional-properties"
    elementFormDefault="qualified" attributeFormDefault="unqualified">

    <element name="objects">
        <complexType>
            <sequence>
                <element name="object" maxOccurs="unbounded">
                    <complexType>
                        <attribute name="name" type="string" />
                        <attribute name="color" type="string" />
                    </complexType>
                </element>
            </sequence>
        </complexType>
        <unique name="UniqueObjectName">
            <selector xpath="tns:object" />
            <field xpath="@name" />
        </unique>
        <unique name="UniqueObjectNameColor">
            <selector xpath="tns:object" />
            <field xpath="@name" />
            <field xpath="@color" />
        </unique>
    </element>

</schema>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文