Page 1 of 1

Validation XML agaist XSD

Posted: Wed Mar 01, 2006 3:44 am
by rajraj
<?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 ?

Posted: Mon Mar 06, 2006 10:50 pm
by jkmyoung
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"

Posted: Wed Mar 08, 2006 12:31 pm
by george
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