Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validating xml with multiple schemas using wildcards 1

Status
Not open for further replies.

vihrao

IS-IT--Management
Jul 25, 2004
18
0
0
US
I am trying to validate a xml file with two schema files cust.xsd and cust1.xsd. The schema file cust.xsd allows addition of elements from another schema cust1.xsd by using xs:any wildcard.

cust.xsd – schema
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="urn:xmlns:sysapex-com:customer" elementFormDefault="qualified"
xmlns="urn:xmlns:sysapex-com:customer" xmlns:mstns="urn:xmlns:sysapex-com:customer" xmlns:xs=" <xs:element name="Customer">
<xs:complexType>
<xs:sequence>
<xs:element name="Address" type="xs:string" />
<xs:element name="City" type="xs:string" />
<xs:element name="State" type="xs:string" />
<xs:element name="Zip" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

cust1.xsd - schema
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema
targetNamespace="urn:xmlns:sysapex-com:address" elementFormDefault="qualified"
xmlns="urn:xmlns:sysapex-com:address"
xmlns:mstns="urn:xmlns:sysapex-com:address"
xmlns:xs=" <xs:element name="Customer">
<xs:complexType>
<xs:sequence>
<xs:element name="Address" type="xs:string" />
<xs:element name="City" type="xs:string" />
<xs:element name="State" type="xs:string" />
<xs:element name="Zip" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

cust.xml - This has to be validated against both cust.xsd and cust1.xsd

<cust:Customer customerID="12345" numPurchases="17"
xmlns:xsi=" xmlns:cust="urn:xmlns:sysapex-com:customer"
xsi:noNamespaceSchemaLocation="file://c:/per/Schemas_nn/new1/cust.xsd"

xsi:schemaLocation="urn:xmlns:sysapex-com:address file://c:/per/Schemas_nn/new1/cust1.xsd"
xmlns:addr="urn:xmlns:sysapex-com:address">


<cust:FirstName >Dare</cust:FirstName>
<cust:LastName >Obasanjo</cust:LastName>
<cust:phoneNumber >425-555-1234</cust:phoneNumber>

<addr:Address>2001 Beagle Drive</addr:Address>
<addr:City>Redmond</addr:City>
<addr:State>WA</addr:State>
<addr:Zip>98052</addr:Zip>

</cust:Customer>

Both schemas are OK. However when I validate cust.xml with the schemas, I get the following error in MSXML parser using VS Studio 2003. “Although this XML document is well-formed it contains a structure that data view cannot display. The urn:xmlns:syspaex-com:address:Adress element is not declared”.

I think I have a syntax mistake. Can someone tell me what is wrong?

I really appreciate your help!
 
Proof-read your cust.xsd and repost what you really meant for it? I don't see xs:any and it won't validate cust.xml's corresponding section.
 
I apologize I posted the wrong cust.xsd. I am reposting the cust.xsd. Gives me same validation error on cust.xml

cust.xsd
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="urn:xmlns:sysapex-com:customer" elementFormDefault="qualified"
xmlns="urn:xmlns:sysapex-com:customer" xmlns:mstns="urn:xmlns:sysapex-com:customer" xmlns:xs=" <xs:element name="Customer">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="xs:string" />
<xs:element name="LastName" type="xs:string" />

<xs:any namespace="##targetNamespace" processContents="strict"
minOccurs="0" maxOccurs="unbounded" />
<xs:any namespace="##any" processContents="lax" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="customerID" type="xs:integer" />
<xs:anyAttribute namespace="##any" processContents="skip" />
</xs:complexType>
</xs:element>
</xs:schema>

I have suspicion that the xsi:schemaLocation syntax in my cust.xml referring to the path location of my schema file on the c drive as file://c:/per/Schemas_nn/new1/cust1.xsd is causing the error. I know the cust1.xsd is in the right location.

I don't know any better.

Please advise.
 
[1]
>I have suspicion that the xsi:schemaLocation syntax in my cust.xml referring to the path location of my schema file on the c drive as file://c:/per/Schemas_nn/new1/cust1.xsd is causing the error. I know the cust1.xsd is in the right location.
That should not be the problem.

[1.1] But, of course, the file should be there to having a chance to validate the elements purported to be defined in the cust1.xsd. I say that because you use "lax" for address namespace validation. If the processor does not find the file, it will skip it. If you use "strict", the file must be there. This statement must be understood this way. The attributes of schemaLocation and noNamespaceSchemaLocation can be overriden by processing application/script. The above suppose no such thing happening.

[1.2] And then, the xsd must be constructed properly. At present it is not.

[2] The cust1.xsd should construct differently. You should defined those element Address, City, State and Zip as global elements.
[tt]
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema
targetNamespace="urn:xmlns:sysapex-com:address" elementFormDefault="qualified"
xmlns="urn:xmlns:sysapex-com:address"
xmlns:mstns="urn:xmlns:sysapex-com:address"
xmlns:xs=" <xs:element name="Address" type="xs:string" />
<xs:element name="City" type="xs:string" />
<xs:element name="State" type="xs:string" />
<xs:element name="Zip" type="xs:string" />
</xs:schema>
[/tt]
and that's all it should take. From there, you can even use "strict" instead of "lax" to validate the document.

[3] I think it is a bit of luck you've two namespaces only to work with to warrant a structure of the kind to work through. More general case would need xs:import cust1.xsd into cust.xsd and work with the cust.xsd alone as schema.

There might be other things I left out - there are many important details all working together to make it right, - but those are the main points.
 
Thanks a lot.
I did everything you said and it worked.
1) Changed the processContenets to strict
2) Defined all elements in cust1.xsd as global (otherwise gives error The given name Customer matches at least two names in the collection object with different namespaces)
3) Imported cust.xsd into cust1.xsd
4) Changed cust.xml - xsi:noNamespaceSchemaLocation pointing to cust1.xsd.




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top