将属性绑定到其他现有属性

时间:2012-03-28 09:05:52

标签: xsd

我想创建一个XSD。 XSD中的一个可选元素是<PrintoutSettings OrderSource="NameOfOrder_X" .../>

XSD的另一个子结构是

<Order Name="NameOfOrder">...</Order>
<Order Name="NameOfOrder2">...</Order>
...

我的目标是,NameOfOrder_X的属性<PrintoutSettings .../>必须是在<Order>...</Order> - 元素之一中定义的字符串。

我如何在XSD中实现它?

亲切的问候 SB

---

---


这里有一些我的XSD:

<xs:complexType name="Order">
    <xs:sequence>
        <xs:element form="qualified" minOccurs="0" name="Documents" type="Documents"/>
    </xs:sequence>
    <xs:attribute name="Name" type="xs:string" use="required"/>
    <xs:attribute name="Translator" type="xs:string" use="required"/>
    <xs:attribute name="Proofreader" type="xs:string" use="required"/>
    <xs:attribute name="LockedBy" type="xs:string" use="optional"/>
</xs:complexType>


<xs:complexType name="Orders">
    <xs:sequence>
        <xs:element maxOccurs="unbounded" name="Order" type="Order"/>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="QQDBData">
    <xs:sequence>
        <xs:element minOccurs="0" name="Orders" type="Orders"/>
        <xs:element minOccurs="0" name="LocalSettings" type="Settings"/>
    </xs:sequence>
    <xs:attribute default="2000-01-01" name="CreationDate" type="xs:date" use="optional"/>
</xs:complexType>

<xs:element name="QQDBData" type="QQDBData">
    <xs:key name="PK-Orders">
        <xs:selector xpath="Order"/>
        <xs:field xpath="@Name"/>
    </xs:key>
    <xs:keyref name="FK-PrintoutSettings" refer="PK-Orders">
        <xs:selector xpath="PrintoutSettings"/>
        <xs:field xpath="@OrderSource"/>
    </xs:keyref>
</xs:element>

2 个答案:

答案 0 :(得分:2)

您可以为xs:ID使用<Order>的属性类型,为xs:IDREF使用属性类型<PrintoutSettings>

示例XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="doc">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="Order"/>
        <xs:element ref="PrintoutSettings"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Order">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:NMTOKEN">
          <xs:attribute name="Name" use="required" type="xs:ID"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
  <xs:element name="PrintoutSettings">
    <xs:complexType>
      <xs:attribute name="OrderSource" use="required" type="xs:IDREF"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

INVALID XML的示例:(在Xerces中给出错误“IDREF'NameOfOrder_X'没有ID / IDREF绑定。”

<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="book.xsd">
  <Order Name="NameOfOrder">...</Order>
  <Order Name="NameOfOrder2">...</Order>
  <PrintoutSettings OrderSource="NameOfOrder_X"/>
</doc>

VALID XML示例:

<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:noNamespaceSchemaLocation="book.xsd">
  <Order Name="NameOfOrder">...</Order>
  <Order Name="NameOfOrder2">...</Order>
  <PrintoutSettings OrderSource="NameOfOrder2"/>
</doc>

答案 1 :(得分:2)

你从@DevNull得到的答案在技术上是正确的(+1);然而,它不是XSD;查看架构入门中的this section,了解您对ID / IDREF的限制。

我将采用DevNull提出的架构并对其进行修改,以向您展示依赖于key / keyref的推荐XSD方法。

更新:我采用了您的架构并进行了更新,以确保它足以进行我的测试:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:complexType name="Order">
        <xs:sequence>
            <xs:element form="qualified" minOccurs="0" name="Documents" type="Documents"/>
        </xs:sequence>
        <xs:attribute name="Name" type="xs:string" use="required"/>
        <xs:attribute name="Translator" type="xs:string" use="required"/>
        <xs:attribute name="Proofreader" type="xs:string" use="required"/>
        <xs:attribute name="LockedBy" type="xs:string" use="optional"/>
    </xs:complexType>
    <xs:complexType name="Documents"/>
    <xs:complexType name="Orders">
        <xs:sequence>
            <xs:element maxOccurs="unbounded" name="Order" type="Order"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="QQDBData">
        <xs:sequence>
            <xs:element minOccurs="0" name="Orders" type="Orders"/>
            <xs:element minOccurs="0" name="LocalSettings" type="Settings"/>
        </xs:sequence>
        <xs:attribute default="2000-01-01" name="CreationDate" type="xs:date" use="optional"/>
    </xs:complexType>
    <xs:complexType name="Settings">
        <xs:attribute name="OrderSource" use="required" type="xs:string"/>
    </xs:complexType>
    <xs:element name="QQDBData" type="QQDBData">
        <xs:key name="PK-Orders">
            <xs:selector xpath="Orders/Order"/>
            <xs:field xpath="@Name"/>
        </xs:key>
        <xs:keyref name="FK-PrintoutSettings" refer="PK-Orders">
            <xs:selector xpath="LocalSettings"/>
            <xs:field xpath="@OrderSource"/>
        </xs:keyref>
    </xs:element>
</xs:schema> 

我必须做的是更新选择器,因为通过引入其他标签,XPath已经改变。我总是尝试可视化约束,以确保所描述的XPath仍然有意义。

Key-KeyRef

更新的示例XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<QQDBData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" CreationDate="2000-01-01">
    <Orders>
        <Order Name="Name1" Translator="Translator1" Proofreader="Proofreader1" LockedBy="LockedBy1">
            <Documents/>
        </Order>
        <Order Name="Name1" Translator="Translator1" Proofreader="Proofreader1" LockedBy="LockedBy1">
            <Documents/>
        </Order>
    </Orders>
    <LocalSettings OrderSource="OrderSource1"/>
</QQDBData>

错误消息,对于相同的无效XML可能看起来像(我正在使用QTAssistant来验证):

Error occurred while loading [], line 12 position 3 The key sequence 'OrderSource1' in Keyref fails to refer to some key.