XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 29th, 2009, 04:42 PM
TheJim01's Avatar
TheJim01 TheJim01 is offline
if(a==b && b==c) a=c;
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2004
Location: http://middle.nowhere.com
Posts: 2,017 TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)  Folding Points: 86414 Folding Title: Advanced FolderFolding Points: 86414 Folding Title: Advanced FolderFolding Points: 86414 Folding Title: Advanced FolderFolding Points: 86414 Folding Title: Advanced FolderFolding Points: 86414 Folding Title: Advanced Folder
Time spent in forums: 3 Weeks 2 Days 3 h 32 m 25 sec
Reputation Power: 2334
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:
Original - XML Code
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  3. targetNamespace="http://www.blahblahblah.tv/NS"
  4. xmlns="http://www.blahblahblah.tv/NS"
  5. elementFormDefault="qualified">
  6.  
  7.   <xs:complexType name="somethingType">
  8.     <xs:attribute name="id" type="xs:nonNegativeInteger"/>
  9.   </xs:complexType>
  10.  
  11.   <xs:complexType name="rootType">
  12.     <xs:sequence>
  13.       <xs:element name="something" type="somethingType" maxOccurs="unbounded"/>
  14.     </xs:sequence>
  15.   </xs:complexType>
  16.  
  17.   <xs:element name="root" type="rootType">
  18.     <xs:key name="blahKey">
  19.       <xs:selector xpath="./something"/>
  20.       <xs:field xpath="@id"/>
  21.     </xs:key>
  22.   </xs:element>
  23.  
  24. </xs:schema>


test.xml
XML Code:
Original - XML Code
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns="http://www.blahblahblah.tv/NS"
  5. xsi:schemaLocation="http://www.blahblahblah.tv/NS test.xsd">
  6.  
  7. <something id="0" />
  8. <something id="1" />
  9. <something id="1" />
  10.  
  11. </root>

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?

Reply With Quote
  #2  
Old May 30th, 2009, 12:45 PM
xml-profi xml-profi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 120 xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 11 h 42 m 53 sec
Reputation Power: 44
Send a message via ICQ to xml-profi
<xsd:attribute name="id" type="xsd:ID" use="required"/>


xsd:ID

A1
not start with numbers
Comments on this post
TheJim01 agrees: Danke sch&ouml;n
__________________
Helmut Hagemann Germany

wer lesen und google kann ist klar im Vorteil
who can read and google is a clear advantage

Reply With Quote
  #3  
Old June 1st, 2009, 09:31 AM
TheJim01's Avatar
TheJim01 TheJim01 is offline
if(a==b && b==c) a=c;
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2004
Location: http://middle.nowhere.com
Posts: 2,017 TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)  Folding Points: 86414 Folding Title: Advanced FolderFolding Points: 86414 Folding Title: Advanced FolderFolding Points: 86414 Folding Title: Advanced FolderFolding Points: 86414 Folding Title: Advanced FolderFolding Points: 86414 Folding Title: Advanced Folder
Time spent in forums: 3 Weeks 2 Days 3 h 32 m 25 sec
Reputation Power: 2334
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.

Reply With Quote
  #4  
Old June 2nd, 2009, 05:47 AM
xml-profi xml-profi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 120 xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 11 h 42 m 53 sec
Reputation Power: 44
Send a message via ICQ to xml-profi
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

Reply With Quote
  #5  
Old June 2nd, 2009, 10:37 AM
TheJim01's Avatar
TheJim01 TheJim01 is offline
if(a==b && b==c) a=c;
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2004
Location: http://middle.nowhere.com
Posts: 2,017 TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)TheJim01 User rank is General 20th Grade (Above 100000 Reputation Level)  Folding Points: 86414 Folding Title: Advanced FolderFolding Points: 86414 Folding Title: Advanced FolderFolding Points: 86414 Folding Title: Advanced FolderFolding Points: 86414 Folding Title: Advanced FolderFolding Points: 86414 Folding Title: Advanced Folder
Time spent in forums: 3 Weeks 2 Days 3 h 32 m 25 sec
Reputation Power: 2334
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:
Original - XML Code
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  4. targetNamespace="http://blah.com/NS"
  5. xmlns="http://blah.com/NS"
  6. xmlns:tns="http://blah.com/NS"
  7. elementFormDefault="qualified">
  8.  
  9. <xs:element name="root" type="rootType">
  10.   <xs:key name="someKey">
  11.     <xs:selector xpath="./tns:someNode"/>
  12.     <xs:field xpath="@uniqueId"/>
  13.   </xs:key>
  14. </xs:element>
  15.  
  16. <xs:complexType name="rootType">
  17.   <xs:sequence>
  18.     <xs:element name="someNode" type="someType" minOccurs="1" maxOccurs="unbounded"/>
  19.   </xs:sequence>
  20. </xs:complexType>
  21.  
  22. <xs:complexType name="someType">
  23.   <xs:attribute name="uniqueId" type="xs:nonNegativeInteger"/>
  24. </xs:complexType>
  25.  
  26. </xs:schema>


test.xml:
XML Code:
Original - XML Code
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns="http://blah.com/NS"
  5. xsi:schemaLocation="http://blah.com/NS asdf.xsd">
  6.  
  7. <someNode uniqueId="0"/>
  8. <someNode uniqueId="1"/>
  9. <someNode uniqueId="2"/>
  10.  
  11. </root>

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > N00b question: unique attributes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek