XML Schema唯一键

时间:2012-04-11 05:42:55

标签: xml schema unique

我无疑是Schema的新手,我试图让一个特定的属性在一个集合中是唯一的。我还需要将此值作为键,因为它将在别处引用。

这是我目前的架构:

 <xs:element name="subroutines" type="guisubroutines_Type" minOccurs="1">

    <xs:key name="Subroutine_Key">
      <xs:selector xpath="subroutine"/>
      <xs:field xpath="@name"/>
    </xs:key>

    <xs:unique name="Subroutine_Unique">
      <xs:selector xpath="subroutine"/>
      <xs:field xpath="@name"/>
    </xs:unique>

  </xs:element>

  <xs:complexType name="guisubroutines_Type" >
    <xs:sequence>
      <xs:element name="subroutine" type="subroutine_Type" minOccurs="0" maxOccurs="unbounded"/>        
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="subroutine_Type">
    <xs:attribute name="name" type="xs:string" use="required"/>
  </xs:complexType>

这是我的xml:

  <subroutines>
    <subroutine name="one"></subroutine>
    <subroutine name="one"></subroutine>
  </subroutines>

我目前正在使用Visual Studio 2010,它说这个xml是有效的,但如果名称不存在则会发出警告。如果两个名字相同,我希望它发出警告。

有人可以指出我正确的方向吗?

感谢您的时间。

1 个答案:

答案 0 :(得分:4)

所以我在看完之后想出来了:

http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/25603e1c-d7dd-48d2-a9f6-8d4177845441/

快速回答是您需要将命名空间添加到xpath表达式中。 不确定这是一个VS的事情还是只是一般,但是......

<xs:key name="Subroutine_Key">
   <xs:selector xpath="mstns:subroutine"/>
   <xs:field xpath="@name"/>
</xs:key>

修正了我遇到的问题。

再次感谢你的时间,我为浪费它而道歉。

相关问题