|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||||||
|
||||||||
|
N00b question: unique attributes
What I'm trying to do: Provide a schema-based method of creating unique values of my "id" attribute. I'll be referencing them later.
I've been to the end of Google and back, and as far as I can tell I'm creating a valid schema for limiting the ids, but when I try to break the XML, it continues to validate. I validated my schema with the W3C validator. I validate my XML with XMLCopy, and have the schema associated with the XML document. Here are my files: test.xsd XML Code:
test.xml XML Code:
The XML file validates, even though my key should force the "id" attribute of "something" elements to be unique. They're very simple, so I'm obviously missing something trivial--I just can't put my finger on it. Is my validator not working correctly? If so, what would you recommend? |
|
#2
|
|||
|
|||
|
<xsd:attribute name="id" type="xsd:ID" use="required"/>
xsd:ID A1 not start with numbers
__________________
Helmut Hagemann Germany wer lesen und google kann ist klar im Vorteil who can read and google is a clear advantage |
|
#3
|
||||
|
||||
|
I understand that your suggestion implements the standard formatting for IDs, and forces each to be unique (based on the NCName data type from DTD). However, I would like to use a number as my unique ID, which means I can't use this datatype. As an example, consider detailing a list of books, where each book's IBSN will be its unique identifier. This usage would not allow the use of xsd:ID for the unique identifier field.
Every tutorial I've seen indicates this is possible by using xsd:unique or xsd:key to indicate an element or attribute is unique, regardless of data type. I chose xsd:key because I will be referencing my data at a later point. When that didn't work, I tried using xsd:unique, but that validates the bad XML just like xsd:key. I suppose an important aspect to consider is my attribute does not need to use the name "id". For all purposes, the attribute could be named "isbn", "modelNumber", or "telephoneNumber"--it really doesn't matter. All that matters is that I have a numbers-only field that validates unique values. |
|
#4
|
|||
|
|||
|
better answer
hello again
http://www.zvon.org/xxl/XMLSchemaTu...r_keys_st4.html learning by doing Code:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<xsd:element name="root" type="myList">
<xsd:unique name="myId">
<xsd:selector xpath="./a"/>
<xsd:field xpath="id"/>
</xsd:unique>
<xsd:unique name="myart">
<xsd:selector xpath="./a"/>
<xsd:field xpath="@art"/>
</xsd:unique>
</xsd:element>
<xsd:complexType name="myList">
<xsd:sequence minOccurs="1">
<xsd:element name="a" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence minOccurs="1">
<xsd:element name="id" type="xsd:NCName" nillable="true"/>
</xsd:sequence>
<xsd:attribute name="art" type="xsd:nonNegativeInteger" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Code:
<?xml version="1.0" encoding="UTF-8"?> <root xsi:noNamespaceSchemaLocation="einzig.xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <a art="1"> <id>a</id> </a> <a art="2"> <id>f</id> </a> <a art="3"> <id>d</id> </a> </root> Helmut Hagemann |
|
#5
|
||||||||
|
||||||||
|
I agree, learning by doing is a very good way to learn.
I re-wrote the XSD and XML. After trying several methods of forcing validation errors, I found a significant difference between code that correctly enforces the key, and code that doesn't. The issue lies in the namespaces. For my first attempt, I declared a target namespace in the schema, and referenced that namespace in the XML file (as I did above). The schema would enforce types, for example xsd:nonNegativeInteger would cause id="-1" to cause an error. The schema would not enforce keys, for example two elements of the same type with id="1" would not cause an error. When I removed the namespace from the schema, and used xsi:noNamespaceSchemaLocation in the XML, it enforced both types and keys. I found it hard to believe that keys are intended to be implemented without namespaces, so I did some more digging. I found that xpath tends to ignore default namespaces. To fix this, I updated my schema with a declared namespace, pointing to the default namespace. I then updated my xpath variables with the named default namespace prefix, and validation was successful. I also noticed that I only needed to add the prefix to the selector path, but that could have been a fluke in my simple schema. My final working example is below: test.xsd: XML Code:
test.xml: XML Code:
Changing one of the uniqueID values to anything but a positive integer will now cause validation to fail. Thank you very much for your help. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > N00b question: unique attributes |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|