XSD and MSXML4.0

XSD and MSXML4.0

Post by Greg Clousto » Fri, 30 Nov 2001 22:42:16



        Ok, I downloaded and installed MSXML 4.0. I've trying to create a test
app that uses XSD to validate XML, but I can't get any XML to validate,
especially the MS examples. Can anyone tell me why I get this error every
timme, "The namespace provided differs from schema's
'http://tempuri.org/po.xsd' targetNamespace."

        Here is my code:

Private Sub cmdTest_Click()

    Dim clsSchemaCache As New XMLSchemaCache40
    Dim clsXMLDOM As New DOMDocument40

    txtReults.text = ""

    clsSchemaCache.validateOnLoad = False
    clsSchemaCache.Add "", txtSchemaPath.text
    Set clsXMLDOM.schemas = clsSchemaCache

    clsXMLDOM.async = False
    clsXMLDOM.resolveExternals = False
    clsXMLDOM.Load txtXMLPath.text

    txtReults.text = CStr(clsXMLDOM.parseError.errorCode) & ": " &
clsXMLDOM.parseError.reason & ", on line #" & CStr
(clsXMLDOM.parseError.Line)

    Set clsSchemaCache = Nothing
    Set clsXMLDOM = Nothing
End Sub

        Here is the XML:

<?xml version="1.0"?>
<purchaseOrder xmlns="http://tempuri.org/po.xsd" orderDate="1999-10-20">
    <shipTo country="US">
        <name>Alice Smith</name>
        <street>123 Maple Street</street>
        <city>Mill Valley</city>
        <state>CA</state>
        <zip>90952</zip>
    </shipTo>
    <billTo country="US">
        <name>Robert Smith</name>
        <street>8 Oak Avenue</street>
        <city>Old Town</city>
        <state>PA</state>
        <zip>95819</zip>
    </billTo>
    <comment>Hurry, my lawn is going wild!</comment>
    <items>
        <item partNum="872-AA">
            <productName>Lawnmower</productName>
            <quantity>1</quantity>
            <USPrice>148.95</USPrice>
            <comment>Confirm this is electric</comment>
        </item>
        <item partNum="926-AA">
            <productName>Baby Monitor</productName>
            <quantity>1</quantity>
            <USPrice>39.98</USPrice>
            <shipDate>1999-05-21</shipDate>
        </item>
    </items>
</purchaseOrder>

Here is the XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://tempuri.org/po.xsd" xmlns="http:tempuri.org/po.xsd"
elementFormDefault="qualified">
        <xs:annotation>
                <xs:documentation xml:lang="en">
                        Purchase order schema for Example.com.
                        Copyright 2000 Example.com. All rights reserved.
                </xs:documentation>
        </xs:annotation>

        <xs:element name="purchaseOrder" type="PurchaseOrderType"/>
        <xs:element name="comment" type="xs:string"/>

        <xs:complexType name="PurchaseOrderType">
                <xs:sequence>
                        <xs:element name="shipTo" type="USAddress"/>
                        <xs:element name="billTo" type="USAddress"/>
                        <xs:element ref="comment" minOccurs="0"/>
                        <xs:element name="items"  type="Items"/>
                </xs:sequence>
                <xs:attribute name="orderDate" type="xs:date"/>
        </xs:complexType>

        <xs:complexType name="USAddress">
                <xs:sequence>
                        <xs:element name="name"   type="xs:string"/>
                        <xs:element name="street" type="xs:string"/>
                        <xs:element name="city"   type="xs:string"/>
                        <xs:element name="state"  type="xs:string"/>
                        <xs:element name="zip"    type="xs:decimal"/>
                </xs:sequence>
                <xs:attribute name="country" type="xs:NMTOKEN" fixed="US"/>
        </xs:complexType>

        <xs:complexType name="Items">
                <xs:sequence>
                        <xs:element name="item" minOccurs="0"
maxOccurs="unbounded">
                                <xs:complexType>
                                        <xs:sequence>
                                                <xs:element name="productName"
type="xs:string"/>
                                                <xs:element name="quantity">
                                                        <xs:simpleType>
                                                                <xs:restriction
base="xs:positiveInteger">
                                                                        <xs:maxExclusive
value="100"/>
                                                                </xs:restriction>
                                                        </xs:simpleType>
                                                </xs:element>
                                                <xs:element name="USPrice"    
type="xs:decimal"/>
                                                <xs:element ref="comment"
minOccurs="0"/>
                                                <xs:element name="shipDate"
type="xs:date" minOccurs="0"/>
                                        </xs:sequence>
                                        <xs:attribute name="partNum" type="SKU"
use="required"/>
                                </xs:complexType>
                        </xs:element>
                </xs:sequence>
        </xs:complexType>

        <!-- Stock Keeping Unit, a code for identifying products -->
        <xs:simpleType name="SKU">
                <xs:restriction base="xs:string">
                        <xs:pattern value="\d{3}-[A-Z]{2}"/>
                </xs:restriction>
        </xs:simpleType>
</xs:schema>

 
 
 

XSD and MSXML4.0

Post by Sven Groo » Sat, 01 Dec 2001 02:33:40


You need to specify the namespace in the clsSchemaCache.Add method.
So it must be: clsSchemaCache.Add "http://tempuri.org/po.xsd",
txtSchemaPath.text
You can only leave the namespace parameter at "" when the schema contains no
targetNamespace.

On another note, the prefix cls is usually used for the actual class
definition, not for an instantiation of a class. For object instance
prefixes, you can either choose one to match the object's name (like cmd for
the CommandButton class) or just use obj or o.
Not that it's mandatory, but it is a more widely used coding convention so
maybe you want to use it if you want to keep your code as readable as
possible to other developers.

--
Sven Groot

Quote:>     Ok, I downloaded and installed MSXML 4.0. I've trying to create a test
> app that uses XSD to validate XML, but I can't get any XML to validate,
> especially the MS examples. Can anyone tell me why I get this error every
> timme, "The namespace provided differs from schema's
> 'http://tempuri.org/po.xsd' targetNamespace."

>     Here is my code:

> Private Sub cmdTest_Click()

>     Dim clsSchemaCache As New XMLSchemaCache40
>     Dim clsXMLDOM As New DOMDocument40

>     txtReults.text = ""

>     clsSchemaCache.validateOnLoad = False
>     clsSchemaCache.Add "", txtSchemaPath.text
>     Set clsXMLDOM.schemas = clsSchemaCache

>     clsXMLDOM.async = False
>     clsXMLDOM.resolveExternals = False
>     clsXMLDOM.Load txtXMLPath.text

>     txtReults.text = CStr(clsXMLDOM.parseError.errorCode) & ": " &
> clsXMLDOM.parseError.reason & ", on line #" & CStr
> (clsXMLDOM.parseError.Line)

>     Set clsSchemaCache = Nothing
>     Set clsXMLDOM = Nothing
> End Sub

>     Here is the XML:

> <?xml version="1.0"?>
> <purchaseOrder xmlns="http://tempuri.org/po.xsd" orderDate="1999-10-20">
>     <shipTo country="US">
>         <name>Alice Smith</name>
>         <street>123 Maple Street</street>
>         <city>Mill Valley</city>
>         <state>CA</state>
>         <zip>90952</zip>
>     </shipTo>
>     <billTo country="US">
>         <name>Robert Smith</name>
>         <street>8 Oak Avenue</street>
>         <city>Old Town</city>
>         <state>PA</state>
>         <zip>95819</zip>
>     </billTo>
>     <comment>Hurry, my lawn is going wild!</comment>
>     <items>
>         <item partNum="872-AA">
>             <productName>Lawnmower</productName>
>             <quantity>1</quantity>
>             <USPrice>148.95</USPrice>
>             <comment>Confirm this is electric</comment>
>         </item>
>         <item partNum="926-AA">
>             <productName>Baby Monitor</productName>
>             <quantity>1</quantity>
>             <USPrice>39.98</USPrice>
>             <shipDate>1999-05-21</shipDate>
>         </item>
>     </items>
> </purchaseOrder>

> Here is the XSD:

> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> targetNamespace="http://tempuri.org/po.xsd"

xmlns="http:tempuri.org/po.xsd"

- Show quoted text -

Quote:> elementFormDefault="qualified">
> <xs:annotation>
> <xs:documentation xml:lang="en">
>    Purchase order schema for Example.com.
>    Copyright 2000 Example.com. All rights reserved.
>   </xs:documentation>
>   </xs:annotation>

> <xs:element name="purchaseOrder" type="PurchaseOrderType"/>
> <xs:element name="comment" type="xs:string"/>

> <xs:complexType name="PurchaseOrderType">
>   <xs:sequence>
>    <xs:element name="shipTo" type="USAddress"/>
>    <xs:element name="billTo" type="USAddress"/>
>    <xs:element ref="comment" minOccurs="0"/>
>    <xs:element name="items"  type="Items"/>
>   </xs:sequence>
>   <xs:attribute name="orderDate" type="xs:date"/>
> </xs:complexType>

> <xs:complexType name="USAddress">
> <xs:sequence>
> <xs:element name="name"   type="xs:string"/>
> <xs:element name="street" type="xs:string"/>
> <xs:element name="city"   type="xs:string"/>
> <xs:element name="state"  type="xs:string"/>
> <xs:element name="zip"    type="xs:decimal"/>
> </xs:sequence>
>   <xs:attribute name="country" type="xs:NMTOKEN" fixed="US"/>
> </xs:complexType>

> <xs:complexType name="Items">
> <xs:sequence>
> <xs:element name="item" minOccurs="0"
> maxOccurs="unbounded">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="productName"
> type="xs:string"/>
> <xs:element name="quantity">
> <xs:simpleType>
> <xs:restriction
> base="xs:positiveInteger">
> <xs:maxExclusive
> value="100"/>
> </xs:restriction>
> </xs:simpleType>
> </xs:element>
> <xs:element name="USPrice"
> type="xs:decimal"/>
> <xs:element ref="comment"
> minOccurs="0"/>
> <xs:element name="shipDate"
> type="xs:date" minOccurs="0"/>
> </xs:sequence>
> <xs:attribute name="partNum" type="SKU"
> use="required"/>
> </xs:complexType>
> </xs:element>
> </xs:sequence>
> </xs:complexType>

>   <!-- Stock Keeping Unit, a code for identifying products -->
> <xs:simpleType name="SKU">
> <xs:restriction base="xs:string">
> <xs:pattern value="\d{3}-[A-Z]{2}"/>
> </xs:restriction>
> </xs:simpleType>
> </xs:schema>


 
 
 

XSD and MSXML4.0

Post by Juan Manuel Molinar » Sat, 01 Dec 2001 03:41:17


I want to know if someone can help me with the
implementation of the schema files that EAN.UCC publish
over the internet.
I receive errors messages when I try to import them with
the Microsoft? XML Parser (MSXML) 4.0 .
I've already read the documentation available in the site
but I didn't find the answer.
I've tried with "Item.xsd", "Envelope.xsd", "Core.xsd",
and so on.
I send the "Envelope.xsd" in the attach.
Here are some of the error messages.

Regards.

I am working with VB6 and W2K.

Instruction
XmlSchema.Add "http://www.uc-
council.org/smp/schemas/as2", "c:\temp\xml\Envelope.xsd"

ERROR
'{http://www.uc-council.org/smp/schemas/core}String1Type'
is a duplicate XSD simpleType

Instruction
XmlSchema.Add "http://www.uc-
council.org/smp/schemas/core", "c:\temp\xml\Item.xsd"

ERROR
Invalid Schema : 'reference to undeclared model group'.

Instruction
XmlSchema.Add "http://www.uc-
council.org/smp/schemas/core", "c:\temp\xml\Core.xsd"

ERROR
Invalid Schema : 'undefined complexType is used as a base
for simple content restriction'.

  Envelope.xsd
3K Download
 
 
 

XSD and MSXML4.0

Post by Greg Clousto » Sun, 02 Dec 2001 02:40:17


        I figured the name space part out, but thanks anyway. For the most
part we use the coding standards laid out in * VB, but we do use the
"cls" prefix as a general object prefix instead of "obj". We only use
variable prefixes with at least 3 characters, as is common in most name
structured naming conventions.
 
 
 

1. Validate XML against XSD with MSXML4

Hi,

we're using MSXML4, among others, to validate a xml file against a xsd scheme.
There is an element in the XML-File which is not defined in the XSD, but when calling objXMLDOM.Validate no error is in the errors-collection.

Any idea?
We're using this code:
---
Set objXMLDom = New MSXML2.DOMDocument40
.
objXMLDom.validateOnParse = False
objXMLDom.Load objFile.Path
.
Set objXMLSchema = New MSXML2.XMLSchemaCache40
objXMLSchema.Add "urn:TIC_CHECK_ITEM", "TIC_CHECK_ITEM.xsd"
Set objXMLDom.schemas = objXMLSchema
objXMLDom.Validate
---

2. I-D ACTION:draft-kitamura-ipv6-name-auto-reg-01.txt

3. XSD validation with MSXML4?

4. sendmail FAQ

5. Async Programming with poll/AIO

6. How to get xsd types from SOM (MSXML4)

7. Toolbox Corrupted/Missing icons

8. Does MSXML4 support referencing XSD through "xsi:...SchemaLocation" in instance documents?

9. msxml4, XSD validation and preserveWhiteSpace

10. how validate an XML file against a XSD schema using MSXML4 and a C++ program?

11. How to validate XML against XSD schema using MSXML4.0 and IXML...

12. MSXML4 XSD keyref bug?