2011-10-20 22 views
5

我無法使xs:unique說明符在XML文件中工作。我似乎無法制定出可行的XPath。我很抱歉在這個問題的代碼量,但我會非常感謝任何人誰可以指出我下面做錯了什麼。無論我做什麼,我都無法獲取元素中的@ref屬性來報告重複值的錯誤(每個ref都必須是唯一的)。如何在XML模式中指定唯一值

任何幫助或指向信息將非常gratefully收到。

一種希望,帕特里克

這是我的架構:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="Artworks" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:aw="http://www.fourthwish.co.uk/data/Artworks.xsd" 
xmlns="http://www.fourthwish.co.uk/data/Artworks.xsd" 
targetNamespace="http://www.fourthwish.co.uk/data/Artworks.xsd" 
elementFormDefault="qualified" 
> 
<xs:element name="artworks"> 
    <xs:complexType> 
    <xs:sequence minOccurs="0" maxOccurs="unbounded"> 
      <xs:element name="artwork" type="ArtworkType"> 
       <xs:unique name="uniqueRef"> 
        <xs:selector xpath="artwork"/> 
        <xs:field xpath="@ref"/> 
       </xs:unique> 
      </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 
<xs:complexType name="ArtworkType"> 
    <xs:sequence> 
     <xs:element name="title" type="xs:string"/> 
    </xs:sequence> 
    <xs:attribute name="ref" type="xs:nonNegativeInteger"/> 
</xs:complexType> 
</xs:schema> 

這是我的XML文件:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<artworks 
xmlns="http://www.fourthwish.co.uk/data/Artworks.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.fourthwish.co.uk/data/Artworks.xsd Artworks.xsd" 
> 
<artwork ref="1"> 
    <title>Title String</title> 
</artwork> 
<artwork ref="1"> 
    <title>Title String</title> 
</artwork> 
</artworks> 

爲什麼我不能得到重複的裁判錯誤值? Arrrggghhh!我已經閱讀了互聯網上的所有內容。請幫助別人。

回答

3

使用此:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="Artworks" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:aw="http://www.fourthwish.co.uk/data/Artworks.xsd" 
xmlns="http://www.fourthwish.co.uk/data/Artworks.xsd" 
targetNamespace="http://www.fourthwish.co.uk/data/Artworks.xsd" 
elementFormDefault="qualified" 
> 
    <xs:element name="artworks"> 
    <xs:complexType> 
     <xs:sequence minOccurs="0" maxOccurs="unbounded"> 
     <xs:element name="artwork" type="ArtworkType"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:unique name="uniqueRef"> 
     <xs:selector xpath="aw:artwork"/> 
     <xs:field xpath="@ref"/> 
    </xs:unique> 

    </xs:element> 

    <xs:complexType name="ArtworkType"> 
    <xs:sequence> 
     <xs:element name="title" type="xs:string"/> 
    </xs:sequence> 
    <xs:attribute name="ref" type="xs:nonNegativeInteger"/> 
    </xs:complexType> 
</xs:schema> 
1

如果運行通過撒克遜EE這個模式,它會告訴你:

警告:test.xsd的第13行: 複雜類型ArtworkType不允許名爲{}藝術品的子元素

這基本上告訴你,你忘了說藝術品是在一個名稱空間,因此需要一個前綴。

相關問題