Validation XML agaist XSD

This should cover W3C XML Schema, Relax NG and DTD related problems.
rajraj
Posts: 1
Joined: Wed Mar 01, 2006 3:42 am

Validation XML agaist XSD

Post by rajraj » Wed Mar 01, 2006 3:44 am

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://mydomain.com/Namespaces/Containe ... cation.xsd" xmlns:mh="http://mydomain.com/Namespaces/Containe ... Header.xsd" xmlns:sn="http://mydomain.com/Namespaces/Containe ... cation.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://mydomain.com/Namespaces/Containe ... cation.xsd" elementFormDefault="qualified" version="4.0.00">
<xsd:import namespace="http://mydomain.com/Namespaces/Containe ... Header.xsd" schemaLocation="../../Container/Public/MessageHeader.xsd"/>
<xsd:import namespace="http://mydomain.com/Namespaces/Containe ... cation.xsd" schemaLocation="../../Container/Public/Notification.xsd"/>
<!-- Schema structure -->
<xsd:element name="Notification" type="Notification"/>
<xsd:complexType name="Notification">
<xsd:sequence>
<xsd:element ref="mh:MessageHeader"/>
<xsd:element ref="sn:Notification"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

I have the above XSD ( which imports 2 XSD's )

I am validating the XML against the XSD.. and the XML looks like


<?xml version="1.0" encoding="UTF-8"?>

<Notification>

<mh:MessageHeader>

<mh:TrackingMessageHeader>

<sn:billingSystemId>CARE</sn:billingSystemId>

</mh:TrackingMessageHeader>

</mh:MessageHeader>

<sn:Notification>

<sn:billingMarket>

<sn:billingMarket>35</sn:billingMarket>

</sn:billingMarket>

</sn:Notification>

</Notification>

I am getting the following error

START DOCUMENT

<?xml version='1.0' encoding='UTF-8'?>
** Parsing error SAXParseException, line 8, uri null
src-resolve: Cannot resolve the name 'mh:MessageHeader' to a(n) elment declar
ation component.
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'mh:MessageH
eader' to a(n) elment declaration component.
at weblogic.apache.xerces.parsers.AbstractSAXParser.parse(Lorg/xml/sax/I
nputSource;)V(AbstractSAXParser.java:1189)
at weblogic.xml.jaxp.WebLogicXMLReader.parse(Lorg/xml/sax/InputSource;)V
(WebLogicXMLReader.java:135)
at weblogic.xml.jaxp.RegistryXMLReader.parse(Lorg/xml/sax/InputSource;)V
(RegistryXMLReader.java:152)
at javax.xml.parsers.SAXParser.parse(Lorg/xml/sax/InputSource;Lorg/xml/s
ax/helpers/DefaultHandler;)V(SAXParser.java:345)
at XMLBean.onMessage(Ljava/lang/String;)V(XMLBean.java:67)
at XMLBean.main([Ljava/lang/String;)V(XMLBean.java:112)


This is the Validation Code i have

static final String W3C_XML_SCHEMA = "http://www.w3.org/2002/XMLSchema";
static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";

RequestHandler rh = new RequestHandler();

try {

String txt = uri;
System.out.println(txt);

SAXParserFactory fact = SAXParserFactory.newInstance();

fact.setNamespaceAware(true);
fact.setValidating(true);
fact.setFeature("http://xml.org/sax/features/validation", true);
fact.setFeature("http://apache.org/xml/features/validation/schema",true);
fact.setFeature("http://apache.org/xml/features/validati ... l-checking", true);

SAXParser sp = fact.newSAXParser();

sp.setProperty(JAXP_SCHEMA_SOURCE, W3C_XML_SCHEMA);
sp.setProperty(JAXP_SCHEMA_SOURCE,new File(SchemaUrl));

InputSource inSource = new InputSource(new FileInputStream(txt));
sp.parse(inSource, rh);

}

Why i am getting the error ? Any idea ?

I do not have the access to mydomain.com .. is that a problem ?

jkmyoung
Posts: 89
Joined: Mon Mar 06, 2006 10:13 pm

Post by jkmyoung » Mon Mar 06, 2006 10:50 pm

You need to declare your namespaces in your xml file
eg.
<mh:MessageHeader>
the xml has no idea what mh: represents.
same with sn:

eg you have in your schema file.
xmlns:mh="http://mydomain.com/Namespaces/Containe ... Header.xsd" xmlns:sn="http://mydomain.com/Namespaces/Containe ... cation.xsd"
Last edited by jkmyoung on Wed Mar 08, 2006 5:50 pm, edited 1 time in total.

george
Site Admin
Posts: 2101
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Wed Mar 08, 2006 12:31 pm

And you need to declare namespaces for those prefixes also in the schema file:

<xsd:element ref="mh:MessageHeader"/>
<xsd:element ref="sn:Notification"/>

Best Regards,
George

Post Reply