469,282 Members | 1,666 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

null value in XML

Hi all,
I am having problem when did the validation of XML document with Schema.
my schema is like the following:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="schema.xsd"
xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"
elementFormDefault="qualified" targetNamespace="schema.xsd">
<xsd:element name="bookstore" type="bookstoreType" />
<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string" />
<xsd:element name="author" type="authorName" />
<xsd:element name="price" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string" />
<xsd:attribute name="publicationdate" type="xsd:string" />
<xsd:attribute name="ISBN" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string" />
<xsd:element name="last-name" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

and the xml file is:
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore xmlns = "schema.xsd">
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price></price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>

when I did the validation, gave me a error saying that
Validation error: The 'schema.xsd:price' element has an invalid
value according to its data type.

I know it is because there is a empty element
<price></price>
in my xml file.
if there any solution to use so that the xml XmlValidatingReader can
recognize the null value?

Thanks.

Lynn

Nov 12 '05 #1
3 13336
In the schema put the nillable true attribute on the price element like:

<xsd:element name="price" type="xsd:decimal" nillable="true"/>

and in the XML set the xsi:nil=true but you have to map the xsi namespace
first. Your new XML is:

<bookstore xmlns = "schema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price xsi:nil="true"></price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>
"Lynn" <xi***********@hotmail.com> wrote in message
news:uW**************@tk2msftngp13.phx.gbl...
Hi all,
I am having problem when did the validation of XML document with Schema. my schema is like the following:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="schema.xsd" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"
elementFormDefault="qualified" targetNamespace="schema.xsd">
<xsd:element name="bookstore" type="bookstoreType" />
<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string" />
<xsd:element name="author" type="authorName" />
<xsd:element name="price" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string" />
<xsd:attribute name="publicationdate" type="xsd:string" />
<xsd:attribute name="ISBN" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string" />
<xsd:element name="last-name" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

and the xml file is:
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database --> <bookstore xmlns = "schema.xsd">
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price></price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>

when I did the validation, gave me a error saying that
Validation error: The 'schema.xsd:price' element has an invalid
value according to its data type.

I know it is because there is a empty element
<price></price>
in my xml file.
if there any solution to use so that the xml XmlValidatingReader can
recognize the null value?

Thanks.

Lynn


Nov 12 '05 #2
Thanks Zafar.
That works.
Is that possible that it can work without
xsi:nil attribute?

thanks.

Lynn
"Zafar Abbas [MSFT]" <za****@microsoft.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
In the schema put the nillable true attribute on the price element like:

<xsd:element name="price" type="xsd:decimal" nillable="true"/>

and in the XML set the xsi:nil=true but you have to map the xsi namespace
first. Your new XML is:

<bookstore xmlns = "schema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price xsi:nil="true"></price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>
"Lynn" <xi***********@hotmail.com> wrote in message
news:uW**************@tk2msftngp13.phx.gbl...
Hi all,
I am having problem when did the validation of XML document with

Schema.
my schema is like the following:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns="schema.xsd"
xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"
elementFormDefault="qualified" targetNamespace="schema.xsd">
<xsd:element name="bookstore" type="bookstoreType" />
<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string" />
<xsd:element name="author" type="authorName" />
<xsd:element name="price" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string" />
<xsd:attribute name="publicationdate" type="xsd:string" />
<xsd:attribute name="ISBN" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string" />
<xsd:element name="last-name" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

and the xml file is:
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory

database -->
<bookstore xmlns = "schema.xsd">
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price></price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>

when I did the validation, gave me a error saying that
Validation error: The 'schema.xsd:price' element has an invalid
value according to its data type.

I know it is because there is a empty element
<price></price>
in my xml file.
if there any solution to use so that the xml XmlValidatingReader can
recognize the null value?

Thanks.

Lynn



Nov 12 '05 #3
It coudn't because then it will try to validate an empty value against the
data type xs:decimal, it will throw a validation error. If you want to
validate empty value for the price element, them you should have it typed as
xs:string and then put pattern restrictions on ONLY accept numbers and an
empty value.

HTH,
Zafar

"Lynn" <xi***********@hotmail.com> wrote in message
news:Os**************@TK2MSFTNGP11.phx.gbl...
Thanks Zafar.
That works.
Is that possible that it can work without
xsi:nil attribute?

thanks.

Lynn
"Zafar Abbas [MSFT]" <za****@microsoft.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
In the schema put the nillable true attribute on the price element like:

<xsd:element name="price" type="xsd:decimal" nillable="true"/>

and in the XML set the xsi:nil=true but you have to map the xsi namespace
first. Your new XML is:

<bookstore xmlns = "schema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price xsi:nil="true"></price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>
"Lynn" <xi***********@hotmail.com> wrote in message
news:uW**************@tk2msftngp13.phx.gbl...
Hi all,
I am having problem when did the validation of XML document with

Schema.
my schema is like the following:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns="schema.xsd"
xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"
elementFormDefault="qualified" targetNamespace="schema.xsd">
<xsd:element name="bookstore" type="bookstoreType" />
<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string" />
<xsd:element name="author" type="authorName" />
<xsd:element name="price" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string" />
<xsd:attribute name="publicationdate" type="xsd:string" />
<xsd:attribute name="ISBN" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string" />
<xsd:element name="last-name" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

and the xml file is:
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory

database -->
<bookstore xmlns = "schema.xsd">
<book genre="autobiography" publicationdate="1981"

ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price></price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6"> <title>The Gorgias</title>
<author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>

when I did the validation, gave me a error saying that
Validation error: The 'schema.xsd:price' element has an invalid value according to its data type.

I know it is because there is a empty element
<price></price>
in my xml file.
if there any solution to use so that the xml XmlValidatingReader can
recognize the null value?

Thanks.

Lynn




Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by iStrain | last post: by
102 posts views Thread by junky_fellow | last post: by
99 posts views Thread by Mikhail Teterin | last post: by
13 posts views Thread by Federico Balbi | last post: by
64 posts views Thread by yossi.kreinin | last post: by
15 posts views Thread by khan | last post: by
ADezii
3 posts views Thread by ADezii | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.