2011-10-20 47 views
5

Tôi không thể lấy xs: unique specifier để làm việc trong một tệp XML. Tôi dường như không thể tìm ra XPath hoạt động được. Tôi xin lỗi về số lượng mã trong câu hỏi này, nhưng tôi sẽ vô cùng biết ơn bất cứ ai có thể chỉ ra những gì tôi đang làm sai dưới đây. Không có vấn đề gì tôi làm, tôi không thể có được thuộc tính @ref trong phần tử để báo cáo lỗi cho việc sao chép giá trị của tôi (mỗi ref phải là duy nhất).Cách chỉ định các giá trị duy nhất trong lược đồ XML

Bất kỳ trợ giúp hoặc chỉ dẫn nào về thông tin sẽ rất biết ơn đã nhận được.

Kind muốn, Patrick

Đây là schema của tôi:

<?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> 

Và đây là tập tin XML của tôi:

<?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> 

Tại sao tôi không nhận được một lỗi cho ref trùng lặp giá trị? Arrrggghhh! Tôi đã đọc mọi thứ trên internet. Xin hãy giúp ai đó.

Trả lời

3

Sử dụng này:

<?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

Nếu bạn chạy schema này qua Saxon-EE, nó cho bạn biết:

Cảnh báo: trên dòng 13 của test.xsd: Tổ hợp loại ArtworkType không cho phép phần tử con có tên là {} ảnh minh họa

, điều này về cơ bản là cho bạn biết rằng bạn quên nói rằng tác phẩm nghệ thuật nằm trong một vùng tên, và do đó cần tiền tố.

Các vấn đề liên quan