Namespaces

Element Definitions

As you have seen, elements are defined using the element element. The name attribute is used to specify the name of the element that will appear in the instance document. You can also use the type attribute to indicate what type of data the element can contain. I will enhance the definition of the Amount element to indicate that it can contain only data of the built-in type double:

<?xml version='1.0'?> <schema xmlns='http://www.w3.org/2001/XMLSchema'  targetNamespace='urn:Commerce'>   <element name='Amount' type='double'/> </schema>

Element definitions can also specify whether an element can contain a nil value. You can optionally set the element's nillable attribute to true or false. If the nillable attribute is not specified, the default value is false. For example, when a client calls the PurchaseItem method on my Commerce Web service, I might or might not have access to the pricing information to return the cost of the purchase. If the pricing information is unavailable, I do not want to give away products for free. Therefore, I should return a null value to indicate that pricing was unavailable instead of zero. The following is a modified version of the previous schema that allows the Amount element to contain a null value:

<?xml version='1.0'?> <schema xmlns='http://www.w3.org/2001/XMLSchema'  targetNamespace='urn:Commerce'>   <!-- Response Message (work-in-progress) -->   <element name='Amount' type='double' nillable='true'/> </schema>

To specify that the element within an instance document contains a nil value, you use the xsi:nil attribute:

<?xml version='1.0'?> <Amount xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'  xmlns='urn:Commerce' xsi:nil='true'/>

Because the Amount element is set to nil, the instance document is valid even though the Amount element does not contain a value of type double. It is also worth noting that the xsi:nil attribute applies only to the value of the element and does not apply to its attributes, if any are defined.



Building XML Web Services for the Microsoft  .NET Platform
Building XML Web Services for the Microsoft .NET Platform
ISBN: 0735614067
EAN: 2147483647
Year: 2002
Pages: 94
Authors: Scott Short

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net