468,247 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

NULL Value in XML Schema

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
4 2075


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
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
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
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.

Similar topics

3 posts views Thread by Lynn | last post: by
2 posts views Thread by Pet Matrix. | last post: by
5 posts views Thread by hello | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.