xml - XSD 对特定类型的兄弟元素的属性的唯一约束

标签 xml xpath xsd unique

我有一个结构为 Q&A 的 XML 文档,它遵循以下格式(为清楚起见进行了编辑):

<question>
    <answer id="1">
        <question>
            <answer id="1"/>
            <answer id="2"/>
            <answer id="3"/>
        </question>
    </answer>
    <answer id="2">
        <question>
            <answer id="1"/>
            <answer id="2"/>
        </question>
    </answer>
</question>

我的 XSD 看起来像这样:

<xs:element name="question">
     <xs:complexType>
        <xs:sequence>
            <xs:element name="answer" type="answerType" minOccurs="2" maxOccurs="unbounded">
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:unique name="AnswerIdUnique">
        <xs:selector xpath="./*" />
        <xs:field xpath="@id" />
    </xs:unique>
</xs:element>

<xs:complexType name="answerType">
    <xs:sequence>
        <xs:element ref="question" minOccurs="0" maxOccurs="1" />
    </xs:sequence>
    <xs:attribute name="id" type="xs:token" use="required" />
</xs:complexType>

当然,它比您在上面看到的要多,但这说明了我的问题。 我需要 answer 元素上的 id 属性在同级元素中是唯一的。上面定义的 XSD 强制了兄弟元素中 id 属性的唯一性,但它不区分元素类型。我在唯一约束中尝试了多种选择器和字段,但没有找到有效的组合。 有什么建议吗?

最佳答案

只需将选择器更改为 <xs:selector xpath="answer"/>你会没事的。一般来说,最好避免像 .//* 这样的 XPaths ,如果只是出于性能原因。

这是您提供的 XML 示例的 XML 架构,我认为它以您想要的方式工作:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="question" type="questionType">
        <xs:unique name="AnswerIdUnique">
            <xs:selector xpath="answer"/>
            <xs:field xpath="@id"/>
        </xs:unique>
    </xs:element>
    <xs:complexType name="questionType">
        <xs:sequence>
            <xs:element name="answer" type="answerType" minOccurs="2" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="answerType">
        <xs:sequence>
            <xs:element ref="question" minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:token" use="required"/>
    </xs:complexType>
</xs:schema>

您发布的 XML 符合上述要求;复制任何兄弟答案的 id 会产生验证错误。例如,以下 XML:

<question> 
    <answer id="1"> 
        <question> 
            <answer id="1"/> 
            <answer id="2"/> 
            <answer id="1"/> 
        </question> 
    </answer> 
    <answer id="1"> 
        <question> 
            <answer id="1"/> 
            <answer id="2"/> 
        </question> 
    </answer> 
</question> 

验证后(在 QTAssistant 中,应该类似于 Visual Studio 中的消息,因为它基于相同的技术),这些是错误:

Error occurred while loading [], line 6 position 5
There is a duplicate key sequence '1' for the 'AnswerIdUnique' key or unique identity constraint.
Error occurred while loading [], line 9 position 3
There is a duplicate key sequence '1' for the 'AnswerIdUnique' key or unique identity constraint.
Document1.xml is invalid.

下面是 Visual Studio 2010 的屏幕截图,显示了针对我发布的 XSD 的上述 XML 验证;虽然这些问题无意中被报告为警告,但它们仍然被报告了。

VS2010 showing unique constraint errors

我随机选择了一个在线验证器 ( http://xsdvalidation.utilities-online.info/ ) 并验证了我发布的相同 XML 和 XSD;错误报告为:

org.xml.sax.SAXParseException: Duplicate unique value [1] declared for identity constraint of element "question".org.xml.sax.SAXParseException: Duplicate unique value [1] declared for identity constraint of element "question".

您必须注意的一件事是,当您的 XSD 具有目标 namespace 时;在这种情况下,需要为所有涉及的命名空间定义一个别名,并在您的选择器中使用它们。

更新:以及带有命名空间的 XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://localhost" xmlns="http://localhost" targetNamespace="http://localhost" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="question" type="questionType">
        <xs:unique name="AnswerIdUnique">
            <xs:selector xpath="tns:answer"/>
            <xs:field xpath="@id"/>
        </xs:unique>
    </xs:element>
    <xs:complexType name="questionType">
        <xs:sequence>
            <xs:element name="answer" type="answerType" minOccurs="2" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="answerType">
        <xs:sequence>
            <xs:element ref="question" minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:token" use="required"/>
    </xs:complexType>
</xs:schema>

请注意tns的介绍前缀及其在选择器中的使用。

关于xml - XSD 对特定类型的兄弟元素的属性的唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10356723/

相关文章:

c# - 在 Windows 窗体中选择和更新数据表

xml - preceding-sibling [1] 不是最后一个

java - 如何使用 Selenium 和 Java 在 webtable 单元格内发送文本

java - XSD - 使用多个 "abstract"子元素类型定义 "concrete"基本元素类型

java - 使用绑定(bind)创建对象 <xsd :any> to xml is giving null?

xml - 使用模板复制时如何在 XSLT 中创建元素

c# - 从 XmlSerializer 生成 Xml 时如何控制 Xml 文档中字符串项数组的 XmlElement 名称

java - 如何使用 XSLT 递归将任何 xml 数据转换为 html TableView :

xml - 使用 XML 数据和 XPath 的 SSRS

java - JAXB Java 和 XSD 映射