473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

xsd validation

ray
I have been working on this for a few days now and can't seem to figure
out how to do it or if it is even possible.

I have and excel spreadsheet that when the user clicks a button it
creates the xml and then the user uploads it to a server. What i want
to do is have the xsd file that is referenced in the xml doc validate
the data ie. if it is required, if it is of the right type ect.

Now the xml file may or may not have a value for a field(account_id).
if it has a value i want it to be only of integer. If it has no value
thats ok to.

How do i do this? I have tried several sugestions that i have found on
this board but just can't seem to get it right.
Thanks for anyhelp you can give and thanks for all the help that you
already have.

Ray

My xml file:

<?xml version="1.0" encoding="ISO-8859-1" ?>
- <data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="LeadXSD.xsd">
- <lead>
<Company>ASTI</Company>
<Account_Id />
<Account_Contact_Id >88</Account_Contact_Id >
</lead>
</data>

My xsd file: it is the account_id field that i am interested in.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="data">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="lead">
<xs:complexType>
<xs:sequence>
<xs:element name="Company" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="64" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Account_Contact_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base xs:string"></xs:restriction>
</xs:simpleType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

My partial C# code:
string xmlDocument = xmlFile
XmlTextReader xml = new XmlTextReader(xmlDocument);
XmlValidatingReader xsd = new XmlValidatingReader(xml);
xsd.ValidationType = ValidationType.Schema;
//and validation errors events go to...
xsd.ValidationEventHandler += new
ValidationEventHandler(MyValidationEventHandler);
while (xsd.Read())
{
}
xsd.Close();

Nov 12 '05 #1
5 3233
You need to change the definition of account_id to be constraint by a
pattern:

<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[1-9]*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

<ra*@cape.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I have been working on this for a few days now and can't seem to figure
out how to do it or if it is even possible.

I have and excel spreadsheet that when the user clicks a button it
creates the xml and then the user uploads it to a server. What i want
to do is have the xsd file that is referenced in the xml doc validate
the data ie. if it is required, if it is of the right type ect.

Now the xml file may or may not have a value for a field(account_id).
if it has a value i want it to be only of integer. If it has no value
thats ok to.

How do i do this? I have tried several sugestions that i have found on
this board but just can't seem to get it right.
Thanks for anyhelp you can give and thanks for all the help that you
already have.

Ray

My xml file:

<?xml version="1.0" encoding="ISO-8859-1" ?>
- <data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="LeadXSD.xsd">
- <lead>
<Company>ASTI</Company>
<Account_Id />
<Account_Contact_Id >88</Account_Contact_Id >
</lead>
</data>

My xsd file: it is the account_id field that i am interested in.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="data">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="lead">
<xs:complexType>
<xs:sequence>
<xs:element name="Company" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="64" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Account_Contact_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base xs:string"></xs:restriction>
</xs:simpleType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

My partial C# code:
string xmlDocument = xmlFile
XmlTextReader xml = new XmlTextReader(xmlDocument);
XmlValidatingReader xsd = new XmlValidatingReader(xml);
xsd.ValidationType = ValidationType.Schema;
//and validation errors events go to...
xsd.ValidationEventHandler += new
ValidationEventHandler(MyValidationEventHandler);
while (xsd.Read())
{
}
xsd.Close();

Nov 12 '05 #2
ray
Thanks for the info. I tried it and wam it worked.
Now is there a way to say that if one element has a value that another
element should have one also. if you look at the phone number elements
there are 3 parts. what i need to do now is say if i have one of the 3
it is required to have all 3? possible or just crazy talk from an xml
wanna be:).

Nov 12 '05 #3
I know of no way to specify dependencies like that between independent
elements in XML. However, if you can make the phone number a
sub-element with its own structure, you can specify that all parts must
be there and that the whole phone number element is optional:

<xs:element ... >
<xs:complexType>
<xs:sequence>
<xs:element name="phoneNumber" type="phoneNumberType"
minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="phoneNumber">
<xs:complexType>
<xs:attribute name="areaCode" type="areaCodeType"
use="required" />
<xs:attribute name="exchangeCode" type="exchangeCodeType"
use="requried" />
<xs:attribute name="number" type="phoneNumberNumberType"
use="required" />
</xs:complexType>
</xs:element>

Nov 12 '05 #4
This pattern misses 0's (so a number like 10 won't pass it). Also, it
doesn't allow for negative numbers. The follwoing pattern will work better:

<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[+-]?[0-9]*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

However, these values are still strings (they just look like integers) and
as such they allow values like 00009. You can further modify the pattern to
remove leading zeros (but still allow 0 value):

<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[+-]?([1-9][0-9]*|0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Zafar Abbas [MSFT]" <za****@microsoft.com> wrote in message
news:OC*************@TK2MSFTNGP12.phx.gbl...
You need to change the definition of account_id to be constraint by a
pattern:

<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[1-9]*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

<ra*@cape.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I have been working on this for a few days now and can't seem to figure
out how to do it or if it is even possible.

I have and excel spreadsheet that when the user clicks a button it
creates the xml and then the user uploads it to a server. What i want
to do is have the xsd file that is referenced in the xml doc validate
the data ie. if it is required, if it is of the right type ect.

Now the xml file may or may not have a value for a field(account_id).
if it has a value i want it to be only of integer. If it has no value
thats ok to.

How do i do this? I have tried several sugestions that i have found on
this board but just can't seem to get it right.
Thanks for anyhelp you can give and thanks for all the help that you
already have.

Ray

My xml file:

<?xml version="1.0" encoding="ISO-8859-1" ?>
- <data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="LeadXSD.xsd">
- <lead>
<Company>ASTI</Company>
<Account_Id />
<Account_Contact_Id >88</Account_Contact_Id >
</lead>
</data>

My xsd file: it is the account_id field that i am interested in.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="data">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="lead">
<xs:complexType>
<xs:sequence>
<xs:element name="Company" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="64" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Account_Contact_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base xs:string"></xs:restriction>
</xs:simpleType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

My partial C# code:
string xmlDocument = xmlFile
XmlTextReader xml = new XmlTextReader(xmlDocument);
XmlValidatingReader xsd = new XmlValidatingReader(xml);
xsd.ValidationType = ValidationType.Schema;
//and validation errors events go to...
xsd.ValidationEventHandler += new
ValidationEventHandler(MyValidationEventHandler);
while (xsd.Read())
{
}
xsd.Close();


Nov 12 '05 #5
Sorry, missed the ( ). Here's what the declarations should look like:

<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([+-]?[0-9]*)?"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

and

<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([+-]?([1-9][0-9]*|0)?"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Stan Kitsis [MSFT]" <sk***@microsoft.com> wrote in message
news:41********@news.microsoft.com...
This pattern misses 0's (so a number like 10 won't pass it). Also, it
doesn't allow for negative numbers. The follwoing pattern will work
better:

<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[+-]?[0-9]*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

However, these values are still strings (they just look like integers) and
as such they allow values like 00009. You can further modify the pattern
to remove leading zeros (but still allow 0 value):

<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[+-]?([1-9][0-9]*|0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at http://www.microsoft.com/info/cpyright.htm
"Zafar Abbas [MSFT]" <za****@microsoft.com> wrote in message
news:OC*************@TK2MSFTNGP12.phx.gbl...
You need to change the definition of account_id to be constraint by a
pattern:

<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[1-9]*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

<ra*@cape.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I have been working on this for a few days now and can't seem to figure
out how to do it or if it is even possible.

I have and excel spreadsheet that when the user clicks a button it
creates the xml and then the user uploads it to a server. What i want
to do is have the xsd file that is referenced in the xml doc validate
the data ie. if it is required, if it is of the right type ect.

Now the xml file may or may not have a value for a field(account_id).
if it has a value i want it to be only of integer. If it has no value
thats ok to.

How do i do this? I have tried several sugestions that i have found on
this board but just can't seem to get it right.
Thanks for anyhelp you can give and thanks for all the help that you
already have.

Ray

My xml file:

<?xml version="1.0" encoding="ISO-8859-1" ?>
- <data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="LeadXSD.xsd">
- <lead>
<Company>ASTI</Company>
<Account_Id />
<Account_Contact_Id >88</Account_Contact_Id >
</lead>
</data>

My xsd file: it is the account_id field that i am interested in.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="data">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="lead">
<xs:complexType>
<xs:sequence>
<xs:element name="Company" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="64" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Account_Contact_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base xs:string"></xs:restriction>
</xs:simpleType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

My partial C# code:
string xmlDocument = xmlFile
XmlTextReader xml = new XmlTextReader(xmlDocument);
XmlValidatingReader xsd = new XmlValidatingReader(xml);
xsd.ValidationType = ValidationType.Schema;
//and validation errors events go to...
xsd.ValidationEventHandler += new
ValidationEventHandler(MyValidationEventHandler);
while (xsd.Read())
{
}
xsd.Close();



Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
2
by: wumingshi | last post by:
Hi, When validating an XML instance, sometimes the schema is not enough to expression the validation rules. Additional validation rules may be expressed in an application-specific way. For...
4
by: Tim Meagher | last post by:
I am trying to use both validation controls and to add submit button attributes, but when I add the button attributes, the javascript fpr the validation controls is no longer created for the page. ...
14
by: Matt | last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or Client-Side form validation? Since I think each validator control can select either 1) JavaScript based error dialog or 2)...
6
by: Stephen | last post by:
Hi, the validation controls dont work on Netscape or Mozilla and only on Internet Explorer why? How do i correct this problem? Thanks
7
by: Ryan Ternier | last post by:
We're running a site that has required field validation on the login page. It works fine on our development / test machines. However, when I upload this site to our live server i get this error. ...
5
by: Chris | last post by:
Based upon some prevoius postings on what to do for adding a 'add' row to a datagrid I utilize the footer to create the 'add' row. The only issue is that I have it sharing the 'UpDate_Command' and...
4
by: David Colliver | last post by:
Hi all, I am having a slight problem that hopefully, someone can help me fix. I have a form on a page. Many items on the form have validation controls attached. Also on this form are...
2
by: dustbort | last post by:
I recently had a problem where my required field validator stopped working. But, the page still posted back and tried to insert a record into the database without performing server-side validation....
6
by: Jon Paal | last post by:
validation doesn't fire what's missing ????? /////// ---- code -----/////////////////////////// Sub btnSubmit_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) 'Handles...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.