Solved

XML Validation

Posted on 2002-05-11
14
405 Views
Last Modified: 2013-11-19
I have an asp page which creates an XML Doc from a posted content from an http post.  I want to know how to then validate this xml against a schema I have stored on the server.

  Set objDOC = Server.CreateObject("Microsoft.XMLDom")
 
  objDOC.async = False
  objDOC.Load Request

  Here I now want to validate this objDOC

Any ideas?

Cheers

Dave
0
Comment
Question by:daveamour
  • 9
  • 4
14 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 7004295
Using XDR (MSXML3+)
=========
<%
 Dim oXML, oSchema
 Set oSchema = Server.CreateObject("MSXML2.XMLSchemaCache.3.0")
 oSchema.Add "x-schema:portfolio-schema.xml", _
    Server.MapPath("schemas/portfolio-schema.xml")
 Set oXML = Server.CreateObject("MSXML2.DOMDocument.3.0")
 Set oXML.Schemas = oSchema
 oXML.async = False
 oXML.load Server.MapPath("xml/portfolio.xml")
 If oXML.parseError.errorCode <> 0 Then
  'validation failed
  Response.Write "Error No:- " & _
   oXML.parseError.errorCode & "<br>"
  Response.Write "Reason:- " & _
   oXML.parseError.reason & "<br>"
 Else
  'validation succeeded
 End If
%>

portfolio-schema.xml (XDR)
==========================
<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
           xmlns:dt="urn:schemas-microsoft-com:datatypes">

<ElementType name="date" dt:type="dateTime"/>
<ElementType name="description"/>

<ElementType name="symbol"/>
<ElementType name="name"/>
<ElementType name="price" dt:type="fixed.14.4"/>
<ElementType name="change" dt:type="fixed.14.4"/>
<ElementType name="percent" dt:type="fixed.14.4"/>
<ElementType name="volume" dt:type="fixed.14.4"/>

<ElementType name="portfolio" content="eltOnly">
  <group minOccurs="0" maxOccurs="1">
    <element type="description" />
  </group>
  <group minOccurs="0" maxOccurs="1">
    <element type="date" />
  </group>
  <group minOccurs="1" maxOccurs="*">
    <element type="stock" />
  </group>
</ElementType>

<ElementType name="stock" content="eltOnly" >
  <element type="symbol" />
  <element type="name" />
  <element type="price" />
  <element type="change" />
  <element type="percent" />
  <element type="volume" />
</ElementType>

</Schema>

Ref:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml03202000.asp
~~~~~~~~~=================~~~~~~~~~~~

Using XSD (MSXML4+)
===================
<%
 Dim oSchema, oXML
 Set oSchema = Server.CreateObject("Msxml2.XMLSchemaCache.4.0")
 oSchema.Add "urn:books", _
    Server.MapPath("schemas/book.xsd")
 Set oXML = Server.CreateObject("Msxml2.DOMDocument.4.0")
 Set oXML.Schemas = oSchema
 oXML.async = False
 oXML.load Server.MapPath("xml/books.xml")
 If oXML.parseError.errorCode <> 0 Then
  'validation failed
  Response.Write "Error No:- " & _
   oXML.parseError.errorCode & "<br>"
  Response.Write "Reason:- " & _
   oXML.parseError.reason & "<br>"
 Else
  'validation succeeded
 End If
%>

book.xsd
=========
<xs:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:books"
            xmlns:b="urn:books">
  <xs:element name="catalog" type="b:CatalogData"/>

  <xs:complexType name="CatalogData">
    <xs:sequence>
      <xs:element name="book" type="b:bookdata" minOccurs="0"
      maxOccurs="unbounded"/>
      </xs:sequence>
  </xs:complexType>

  <xs:complexType name="bookdata">
    <xs:sequence>
      <xs:element name="author" type="xs:string"/>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="genre" type="xs:string"/>
      <xs:element name="price" type="xs:float"/>
      <xs:element name="publish_date" type="xs:date"/>
      <xs:element name="description" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:string"/>
  </xs:complexType>
</xs:schema>


Reference:
MSXML4 SDK
0
 
LVL 19

Author Comment

by:daveamour
ID: 7004365
Ok thanks very much.  I don't realy understand much of this though.

What does XDR mean and why does MSXML3 have a different schema language to MSXML4.

Why does the MSXML3 schema have a file extensions of xml?

What does x-schema mean?

Sorry for all these questions but xml really does baffle me for some reason.

This is my xml file and schema I am trying to work with.  Also I need to be using MSXML3.

<?xml version="1.0" encoding="UTF-8"?>
<LoanCalcResponse>
<LoanCalcItem>
<Element1>£3,400</Element1>
<Element2>36</Element2>
<Element3>11.9%</Element3>
<Element4>£1,360.00</Element4>
<Element5>£90.44</Element5>
<Element6>£79.95</Element6>
</LoanCalcItem>
<LoanCalcItem>
<Element1>£3,400</Element1>
<Element2>36</Element2>
<Element3>11.9%</Element3>
<Element4>None</Element4>
<Element5>£125.17</Element5>
<Element6>£111.85</Element6>
</LoanCalcItem>
<LoanCalcItem>
<Element1>n/a</Element1>
<Element2>n/a</Element2>
<Element3>n/a</Element3>
<Element4>n/a</Element4>
<Element5>n/a</Element5>
<Element6>n/a</Element6>
</LoanCalcItem>
</LoanCalcResponse>


<?xml version="1.0" ?>
<xs:schema id="LoanCalcResponse" targetNamespace="http://tempuri.org/SampleXMLPost.xsd" xmlns:mstns="http://tempuri.org/SampleXMLPost.xsd" xmlns="http://tempuri.org/SampleXMLPost.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
     <xs:element name="LoanCalcResponse" msdata:IsDataSet="true" msdata:Locale="en-GB" msdata:EnforceConstraints="False">
          <xs:complexType>
               <xs:choice maxOccurs="unbounded">
                    <xs:element name="LoanCalcItem">
                         <xs:complexType>
                              <xs:sequence>
                                   <xs:element name="Element1" type="xs:string" minOccurs="0" />
                                   <xs:element name="Element2" type="xs:string" minOccurs="0" />
                                   <xs:element name="Element3" type="xs:string" minOccurs="0" />
                                   <xs:element name="Element4" type="xs:string" minOccurs="0" />
                                   <xs:element name="Element5" type="xs:string" minOccurs="0" />
                                   <xs:element name="Element6" type="xs:string" minOccurs="0" />
                              </xs:sequence>
                         </xs:complexType>
                    </xs:element>
               </xs:choice>
          </xs:complexType>
     </xs:element>
</xs:schema>

Many thanks

Dave



0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7004539
ASP Code
=========
<%@language="VBScript"%>
<%
Dim oSchema, oXML
Set oSchema = Server.CreateObject("Msxml2.XMLSchemaCache.4.0")
oSchema.Add "http://tempuri.org/SampleXMLPost.xsd", Server.MapPath("schema/data.xsd")
Set oXML = Server.CreateObject("Msxml2.DOMDocument.4.0")
Set oXML.Schemas = oSchema
oXML.async = False
oXML.load Server.MapPath("xml/data.xml")
If oXML.parseError.errorCode <> 0 Then
 'validation failed
 Response.Write "Error No:- " & oXML.parseError.errorCode & "<br>"
 Response.Write "Reason:- " & oXML.parseError.reason & "<br>"
Else
 Response.Write "Validation Succeeded"
End If
%>

XML Document 1
============
<?xml version="1.0" encoding="iso-8859-1"?>
<LoanCalcResponse xmlns="http://tempuri.org/SampleXMLPost.xsd">
<LoanCalcItem>
<Element1>#3,400</Element1>
<Element2>36</Element2>
<Element3>11.9%</Element3>
<Element4>#1,360.00</Element4>
<Element5>#90.44</Element5>
<Element6>#79.95</Element6>
</LoanCalcItem>
<LoanCalcItem>
<Element1>#3,400</Element1>
<Element2>36</Element2>
<Element3>11.9%</Element3>
<Element4>None</Element4>
<Element5>#125.17</Element5>
<Element6>#111.85</Element6>
</LoanCalcItem>
<LoanCalcItem>
<Element1>n/a</Element1>
<Element2>n/a</Element2>
<Element3>n/a</Element3>
<Element4>n/a</Element4>
<Element5>n/a</Element5>
<Element6>n/a</Element6>
</LoanCalcItem>
</LoanCalcResponse>

ASP Result 1
============
Validation Succeeded

XML Document 2
===============
<?xml version="1.0" encoding="iso-8859-1"?>
<LoanCalcResponse xmlns="http://tempuri.org/SampleXMLPost.xsd">
<LoanCalcItem>
<Element0>#3,400</Element0>
<Element2>36</Element2>
<Element3>11.9%</Element3>
<Element4>#1,360.00</Element4>
<Element5>#90.44</Element5>
<Element6>#79.95</Element6>
</LoanCalcItem>
<LoanCalcItem>
<Element1>#3,400</Element1>
<Element2>36</Element2>
<Element3>11.9%</Element3>
<Element4>None</Element4>
<Element5>#125.17</Element5>
<Element6>#111.85</Element6>
</LoanCalcItem>
<LoanCalcItem>
<Element1>n/a</Element1>
<Element2>n/a</Element2>
<Element3>n/a</Element3>
<Element4>n/a</Element4>
<Element5>n/a</Element5>
<Element6>n/a</Element6>
</LoanCalcItem>
</LoanCalcResponse>

ASP Result 2
============
Error No:- -1072898028<br>Reason:- Element content is invalid according to the DTD/Schema.
Expecting: {http://tempuri.org/SampleXMLPost.xsd}Element1, {http://tempuri.org/SampleXMLPost.xsd}Element2, {http://tempuri.org/SampleXMLPost.xsd}Element3, {http://tempuri.org/SampleXMLPost.xsd}Element4, {http://tempuri.org/SampleXMLPost.xsd}Element5, {http://tempuri.org/SampleXMLPost.xsd}Element6.
<br>


Notes
=====
1. You have to use namespaces if you have a default namespace and target namespace in your schema element.

2. If you cannot ensure that the XML Document has the matching namespace, then this is what you must do accordingly,

ASP Code II
============
<%@language="VBScript"%>
<%
Dim oSchema, oXML
Set oSchema = Server.CreateObject("Msxml2.XMLSchemaCache.4.0")
oSchema.Add "", Server.MapPath("schema/data.xsd")
Set oXML = Server.CreateObject("Msxml2.DOMDocument.4.0")
Set oXML.Schemas = oSchema
oXML.async = False
oXML.load Server.MapPath("xml/data.xml")
If oXML.parseError.errorCode <> 0 Then
 'validation failed
 Response.Write "Error No:- " & oXML.parseError.errorCode & "<br>"
 Response.Write "Reason:- " & oXML.parseError.reason & "<br>"
Else
 Response.Write "Validation Succeeded"
End If
%>

Schema Document (data.xsd)
==========================
<?xml version="1.0" ?>
<xs:schema id="LoanCalcResponse" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="LoanCalcResponse">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="LoanCalcItem">
<xs:complexType>
<xs:sequence>
<xs:element name="Element1" type="xs:string" minOccurs="0" />
<xs:element name="Element2" type="xs:string" minOccurs="0" />
<xs:element name="Element3" type="xs:string" minOccurs="0" />
<xs:element name="Element4" type="xs:string" minOccurs="0" />
<xs:element name="Element5" type="xs:string" minOccurs="0" />
<xs:element name="Element6" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>


ASP Result With XML Document 1
===============================
Validation Succeeded

ASP Result With XML Document 2
===============================
Error No:- -1072898028<br>Reason:- Element content is invalid according to the DTD/Schema.
Expecting: Element1, Element2, Element3, Element4, Element5, Element6.
<br>

0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7004567
Clarification with regard to ASP Code II (without namespaces)
=======================================

XML Document 1
==============
<?xml version="1.0" encoding="iso-8859-1"?>
<LoanCalcResponse>
<LoanCalcItem>
<Element1>£3,400</Element1>
<Element2>36</Element2>
<Element3>11.9%</Element3>
<Element4>£1,360.00</Element4>
<Element5>£90.44</Element5>
<Element6>£79.95</Element6>
</LoanCalcItem>
<LoanCalcItem>
<Element1>£3,400</Element1>
<Element2>36</Element2>
<Element3>11.9%</Element3>
<Element4>None</Element4>
<Element5>£125.17</Element5>
<Element6>£111.85</Element6>
</LoanCalcItem>
<LoanCalcItem>
<Element1>n/a</Element1>
<Element2>n/a</Element2>
<Element3>n/a</Element3>
<Element4>n/a</Element4>
<Element5>n/a</Element5>
<Element6>n/a</Element6>
</LoanCalcItem>
</LoanCalcResponse>

XML Document 2
================
<?xml version="1.0" encoding="iso-8859-1"?>
<LoanCalcResponse>
<LoanCalcItem>
<Element0>£3,400</Element0>
<Element2>36</Element2>
<Element3>11.9%</Element3>
<Element4>£1,360.00</Element4>
<Element5>£90.44</Element5>
<Element6>£79.95</Element6>
</LoanCalcItem>
<LoanCalcItem>
<Element1>£3,400</Element1>
<Element2>36</Element2>
<Element3>11.9%</Element3>
<Element4>None</Element4>
<Element5>£125.17</Element5>
<Element6>£111.85</Element6>
</LoanCalcItem>
<LoanCalcItem>
<Element1>n/a</Element1>
<Element2>n/a</Element2>
<Element3>n/a</Element3>
<Element4>n/a</Element4>
<Element5>n/a</Element5>
<Element6>n/a</Element6>
</LoanCalcItem>
</LoanCalcResponse>
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7004571
Notes about XDR and XSD
=======================
1. XDR is Microsoft first attempt at crafting Schemas and as such is proprietary.

2. XSD is W3C Standard.

3. MSXML3 supports XDR.

4. MSXML4 supports both XDR and XSD.

5. The schema syntax you are using is XSD.

6. Therefore, you will have to download MSXML4. You will need to install Windows Installer 2.0 before you can successfully install MSXML4 on your web server.
0
 
LVL 19

Author Comment

by:daveamour
ID: 7004622
Ok thanks very much.  I actually have to use MSXML3 for this work though.

This is what I have so far:

File receiving xml post
=======================

  Set objDOC = Server.CreateObject("MSXML2.DOMDocument.3.0")
  Set objSchema = Server.CreateObject("MSXML2.XMLSchemaCache.3.0")
 
  objSchema.Add "x-schema:LoansSchema.xml", "C:\Inetpub\wwwroot\WebLibraries\XML Stuff\LoansClient\LoansSchema.xml"
 
  objDOC.async = False
  objDOC.Load Request
 
  Set objDOC.Schemas = objSchema
 
  If objDOC.parseError.errorCode <> 0 Then
    ErrorMessage = "Error No:- " & objDOC.parseError.errorCode & "<br>" & "Reason:- " & objDOC.parseError.reason & "<br>"
  Else
    'validation succeeded
  End If

My go at an XDR Schema
======================

<?xml version="1.0"?>
<Schema name="LoansSchema" xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <ElementType name="ProductType" content="textOnly" model="closed"/>
  <ElementType name="BorrowAmount" content="textOnly" model="closed"/>
  <ElementType name="LoanTerm" content="textOnly" model="closed"/>
  <ElementType name="Protection" content="textOnly" model="closed"/>
  <ElementType name="Purpose" content="textOnly" model="closed"/>
  <ElementType name="ShowAllResults" content="textOnly" model="closed"/>
  <ElementType name="LoanCalcRequest" content="eltOnly" model="closed">
      <element type="ProductType" minOccurs="1" maxOccurs="1"/>
      <element type="BorrowAmount" minOccurs="1" maxOccurs="1"/>
      <element type="LoanTerm" minOccurs="1" maxOccurs="1"/>
      <element type="Protection" minOccurs="1" maxOccurs="1"/>
      <element type="Purpose" minOccurs="1" maxOccurs="1"/>
      <element type="ShowAllResults" minOccurs="1" maxOccurs="1"/>
      <element type="LoanCalcRequest" minOccurs="1" maxOccurs="1"/>
  </ElementType>
</Schema>

When I deliberatley send invalid xml in the post though, the errorcode is always zero.

Any ideas?

Cheers

Dave
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7005789
Validation Using XDR Without Namespaces
=============================
portfolio.xml
==============
<portfolio>
  <description>Technology Stock Index</description>
  <date>1998-10-13T15:56:00</date>
  <stock>
    <symbol>ACXM</symbol>
    <name>acxiom corp</name>
    <price>18.875</price>
    <change>-1.250</change>
    <percent>-6.21</percent>
    <volume>0.23</volume>
  </stock>
  <stock>
    <symbol>AFFX</symbol>
    <name>affymetrix inc</name>
    <price>20.313</price>
    <change>-1.438</change>
    <percent>-6.61</percent>
    <volume>0.08</volume>
  </stock>
</portfolio>

portfolio-schema.xml
====================
<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
           xmlns:dt="urn:schemas-microsoft-com:datatypes">

<ElementType name="date" dt:type="dateTime"/>
<ElementType name="description"/>

<ElementType name="symbol"/>
<ElementType name="name"/>
<ElementType name="price" dt:type="fixed.14.4"/>
<ElementType name="change" dt:type="fixed.14.4"/>
<ElementType name="percent" dt:type="fixed.14.4"/>
<ElementType name="volume" dt:type="fixed.14.4"/>

<ElementType name="portfolio" content="eltOnly">
  <group minOccurs="0" maxOccurs="1">
    <element type="description" />
  </group>
  <group minOccurs="0" maxOccurs="1">
    <element type="date" />
  </group>
  <group minOccurs="1" maxOccurs="*">
    <element type="stock" />
  </group>
</ElementType>

<ElementType name="stock" content="eltOnly" >
  <element type="symbol" />
  <element type="name" />
  <element type="price" />
  <element type="change" />
  <element type="percent" />
  <element type="volume" />
</ElementType>

</Schema>


ASP Code Using MSXML3
=====================
<%@language="VBScript"%>
<%
Dim oSchema, oXML
Set oSchema = Server.CreateObject("Msxml2.XMLSchemaCache.3.0")
oSchema.Add "", Server.MapPath("portfolio-schema.xml")
Set oXML = Server.CreateObject("Msxml2.DOMDocument.3.0")
Set oXML.Schemas = oSchema
oXML.async = False
oXML.load Server.MapPath("portfolio.xml")
If oXML.parseError.errorCode <> 0 Then
 'validation failed
 Response.Write "Error No:- " & oXML.parseError.errorCode & "<br>"
 Response.Write "Reason:- " & oXML.parseError.reason & "<br>"
Else
 Response.Write "Validation Succeeded"
End If
%>









0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:b1xml2
ID: 7005801
Validation Using Namespaces And XDR
====================================

XML Document
=============
<portfolio xmlns="x-schema:portfolio-schema.xml">
  <description>Technology Stock Index</description>
  <date>1998-10-13T15:56:00</date>
  <stock>
    <symbol>ACXM</symbol>
    <name>acxiom corp</name>
    <price>18.875</price>
    <change>-1.250</change>
    <percent>-6.21</percent>
    <volume>0.23</volume>
  </stock>
  <stock>
    <symbol>AFFX</symbol>
    <name>affymetrix inc</name>
    <price>20.313</price>
    <change>-1.438</change>
    <percent>-6.61</percent>
    <volume>0.08</volume>
  </stock>
</portfolio>


ASP Code
=========
<%@language="VBScript"%>
<%
Dim oSchema, oXML
Set oSchema = Server.CreateObject("Msxml2.XMLSchemaCache.3.0")
oSchema.Add "x-schema:portfolio-schema.xml", Server.MapPath("portfolio-schema.xml")
Set oXML = Server.CreateObject("Msxml2.DOMDocument.3.0")
Set oXML.Schemas = oSchema
oXML.async = False
oXML.load Server.MapPath("portfolio.xml")
If oXML.parseError.errorCode <> 0 Then
 'validation failed
 Response.Write "Error No:- " & oXML.parseError.errorCode & "<br>"
 Response.Write "Reason:- " & oXML.parseError.reason & "<br>"
Else
 Response.Write "Validation Succeeded"
End If
%>








0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 100 total points
ID: 7005816
The code shows you how to validate XML documents against XDR Schemas. This is how u validate XML docs. Now, please dont ask me how to write a schema in this question.
0
 
LVL 19

Author Comment

by:daveamour
ID: 7006002
Thanks for your help.  I still don't have my code working but if being pedantic about the question requirements is whats really important then please just have the points.

Thanks

Dave
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7006154
You asked for the way to validate XML documents using a schema. I have provided a very exhaustive explanation of how to do that via ASP with code that covers both XSD and XDRs.

The least you could do was to express appreciation. Rest assured I will never in my lifetime ever answer a question from you.
0
 
LVL 19

Author Comment

by:daveamour
ID: 7006185
I sais Thanks twice
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 7006211
And really, I care not for your pitance points. It was the technical explanation of how to validate schemas with the given XML Document and XDR as well as XSD Documents with and without namespaces.

If we want to get pedantic about it, I would have not even bothered to answer your question seeing how vague it was. This is a professional web site and we do expect you to know how to write schemas since you were asking how to validate XML Documents.

I have asked for the removal of your points from my profile. I personally don't want to be sullied by a person who shows lack of appreciation (I shall preserve my dignity and that of the site by not using the word that I have used in Community Support)
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7007597
Based on review, grade is changed.

Computer101
E-E Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will learn how to dynamically set the form action using jQuery.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now