The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

XML validation

Hello, is it possible to use XSD validation to make sure that a XML file only has 1 tag of a given type?

<x>
 <xx></xx>
 <xx></xx>
</x>

Like make sure there are no more <x> tags on the file?

Valid
<x>
 <xx>00</xx>
 <xx>11</xx>
</x>

Invalid
<x>
 <xx>00</xx>
 <xx>11</xx>
</x>
<x>
 <xx>01</xx>
 <xx>01</xx>
 <xx>01</xx>
</x>

Thanks.
KrazyMane
Friday, November 09, 2007
 
 
Use the maxOccurs attribute.

<xs:element name="Product" type="xs:string" maxOccurs="1" />
OneMist8k
Friday, November 09, 2007
 
 
Wait.  This is better:

"The default value for both minOccurs and maxOccurs is 1, if they are not provided explicitly.  Therefore, setting minOccurs to 0 means the the element may appear 0 to 1 times.  "

From XML in a Nutshell (O'Reilly)
OneMist8k
Friday, November 09, 2007
 
 
OneMist8k: "Use the maxOccurs attribute."

That doesn't appear to be the answer to the OP's question, though. You're talking about an element (tag) being allowed only once, and is correst. From the OP's sample data, though, the desire is to prevent repeating *content*, which isn't possible.

<element>01</element>
<element>12</element>

would be prohibited by maxOccurs, but from the OP's data that's what they want (the section marked as "valid"). What the OP appears to want, however, is that the above be allowed, but

<element>01</element>
<element>01</element>

would not.

OP: No, AFAIK. Your front end application has to deal with validity of data like that; tht DTD/XSD can deal with "number required, two digits" or "must be true or false", but catn't handle avoiding duplicate content.
Ken White Send private email
Friday, November 09, 2007
 
 
OP said wants no more <x> tags, not <xx> as you've illustrated.  Wouldn't that work, so that <x> appears only once?

Or have I misunderstood?
OneMist8k
Friday, November 09, 2007
 
 
Hey fellas, thanks for your comments.

My question was regarding the number of ocurrences of a tag, not of a value.

Thanks.
KrazyMane
Saturday, November 10, 2007
 
 
If XSD schemas aren't powerful enough to meet your requirements (and given the OP's desription it is difficult to tell), then something like Schematron might be better:

http://www.schematron.com/

Schemas aren't the only way of specifying the structure of XML documents - they are better than DTDs, but that isn't saying much!
Arethuza
Sunday, November 11, 2007
 
 
Why don't you try a "key" constraint. This doesn't match your sample - you have to modify the selector & field elements to suit your own purposes.

<xs:key name="pk_security">
  <xs:selector xpath=".//security"/>
  <xs:field xpath="secidentifier/CUSIP"/>
</xs:key>

See http://www.xml.com/pub/a/2000/11/29/schemas/part1.html?page=6 for further info
Rob J.
Monday, November 12, 2007
 
 
> Hello, is it possible to use XSD validation to make sure that a XML file only has 1 tag of a given type?

Try specifying the maxOccurs attribute, as maxOccurs="1"
Christopher Wells Send private email
Tuesday, November 13, 2007
 
 
Friends don't let friends use XML.
Steve Hirsch Send private email
Saturday, November 17, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz