Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declaring an element empty or >=0 1

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
In a schema, is it possible to declare an element as being either empty or >=0. I can implement the 2nd part without a problem using the type below, but how would I go about ignoring validation rules if the element is left empty?
Code:
<xsd:element name="swGroup" type="xsd:nonNegativeInteger"/>
Your help would be much appreciated!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I think you'll need to use a pattern restriction, something like:
Code:
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="(^\d*$)|(\d+)"/>
  </xs:restriction>
</xs:simpleType>

Jon

"I don't regret this, but I both rue and lament it.
 
Thanks for the reply Jon.
Could you clarify a few points for me please:
1) I understand that the pattern is basically looking for:
a) zero or more digits
OR
b) one or more digits

But what do the ^ and $ symbols mean on the first part of the pattern? I've tried searching a few references but kind find a good clear list of symbols.

2) Should I be using the xsd: prefix or xs: and why?

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
1) Actually, that pattern needs to be changed to simply:
Code:
<xs:pattern value="^\d*$"/>
^ matches the start of string and $ matches the end. I like regular expressions, but I agree, there aren't any good references and it seems like a bit of a dark art.

2) You can use whatever prefix you like (its only a prefix), as long as you specify the namespace to which the prefix belongs.

Jon

"I don't regret this, but I both rue and lament it.
 
Right, I've got the following code in my xsd file now:
Code:
<xsd:element name="swGroup">
 <xsd:simpleType>
  <xsd:restriction base="xsd:string">
   <xsd:pattern value="^\d*$"/>
  </xsd:restriction>
 </xsd:simpleType>
</xsd:element>
But I get a complaint (The content of element swGroup is not valid according to its type definition) when I use it like this in my xml file:
Code:
<swGroup></swGroup>
If I try entering any number in there I still get the same complaint. Is there something I'm missing?

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Try this:
Code:
<xsd:pattern value="([0-9])*"/>

Jon

"I don't regret this, but I both rue and lament it.
 
Ah I see, that works great! Many thanks Jon!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top