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!

XML Schema-Required elements?

Status
Not open for further replies.

SMerrill

Programmer
Jan 19, 2002
145
US
How do I create an element with required content?
That is,
<root>
<item>
<RequiredField></RequiredField>
<Name>A</Name>
</item>
<item>
<RequiredField>Value</RequiredField>
<Name>B</Name>
</item>
</root>

I want this to not validate, because the first item did not have any content.
 
Excellent question.

For string data types we can use <minLength value=&quot;1&quot;/>, but what about all the other datatypes?

Code:
<element name=&quot;RequiredField&quot;>
  <simpleType>
    <restriction base=&quot;string&quot;>
      <minLength value=&quot;1&quot;/>
    </restriction>
  </simpleType>
</element>

I tested this with MSXML 4 and xsv.

When the element is empty -

MSXML reports The element: '{urn:htd}RequiredField' has an invalid value according to its data type.

but xsv finds no errors.
 
Your answer yields a partial answer, and another follow-on question.
For a floating-point type, I would think that something in the following XSD specification would yield a required floating-point number, but I have not been successful in getting it to work:
[tt]
<xsd:element name=&quot;RequiredField&quot; nillable=&quot;false&quot;>
<xsd:simpleType>
<xsd:restriction base=&quot;xsd:float&quot;>
<xsd:pattern value=&quot;\p{N}&quot;/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
[/tt]
I know there are specifications within the xsd:pattern value attribute that indicate optional and required values.
Is there anyone out there who understands this attribute well enough to get it to work?
--Shaun
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top