By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

NULL Value in XML Schema

P: n/a
Hi All,
How to define an XML element with no value and no attribute?

<X>
<Y/>
</X>

And Y has no value?

I've done it with:

<xsi:element name="Y" type="rn:T_NULL"/>

<xsi:simpleType name="rn:T_NULL">
<xsi:restriction base="xsi:string" length="0"/>
</xsi:simpleType>

But it seems too complicated.
Thanks, Reuven
Mar 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a


Reuven Nisser wrote:

How to define an XML element with no value and no attribute?

<X>
<Y/>
</X>

And Y has no value?

I've done it with:

<xsi:element name="Y" type="rn:T_NULL"/>

<xsi:simpleType name="rn:T_NULL">
<xsi:restriction base="xsi:string" length="0"/>
</xsi:simpleType>


The W3C XML schema language offers e.g.

<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:element name="Y" type="xs:string" nillable="true" />

then in the instance document you would need e.g.

<Y xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:nil="true" />

see
<http://www.w3.org/TR/xmlschema-0/#Nils

But that is a mechansism provided more to cater for the possibility that
you can have some Y elements that have contents and some others that
don't have content (have null or nil contents).
If you really want to have all occurences of Y to have no contents then
I guess your approach is fine.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 16 '06 #2

P: n/a
Thanks for Martin's informative inputs.

Hi Reuven,

The nillable="true" and xsi:nil="true" is the standard approach to mark
an element as null value in XML document. However, for your scenario, if
you want the element always be null value(actually it is called an empty
element), we still need to use the complex type definition to restrict the
type's content. In addition to the means you currently use, you can also
have a look at the following article which mentioned some other definition
for empty element in XSD:

#XSD Complex Empty Elements
http://www.w3schools.com/schema/sche...plex_empty.asp

Hope this also helps some.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Mar 17 '06 #3

P: n/a
Hi Steven,
Thanks, this is the answer I was looking for :

<xsi:complexType name="T_NULL">
<xsi:annotation><xsi:documentation>No value allowed
here</xsi:documentation></xsi:annotation>
</xsi:complexType>

Regards, Reuven
Mar 18 '06 #4

P: n/a
You're welcome Reuven,

Have a good day!

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Mar 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.