java_developer
11-24-2009, 07:59 AM
Hi
I am using JAva APi for validating XML using xsd
1)XML is
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="spl.xsl"?>
<d>
<document type="test">123</document>
</d>
2) xsd is
<?xml version="1.0" encoding="ASCII"?>
<xs:schema targetNamespace="urn:hl7-org:v3" xmlns:mif="urn:hl7-org:v3/mif" xmlns="urn:hl7-org:v3" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="d">
<xs:complexType>
<xs:sequence>
<xs:element ref="document" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="document">
<xs:complexType mixed="true">
<xs:attribute name="type" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
3)java code
SchemaFactory factory
= SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
File schemaLocation = new File("C:/workspace/unZip/Test1.xsd");
Schema schema = factory.newSchema(schemaLocation);
Validator validator = schema.newValidator();
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File(args[0]));
DOMSource source = new DOMSource(doc);
DOMResult result = new DOMResult();
try {
validator.validate(source, result);
Document augmented = (Document) result.getNode();
// do whatever you need to do with the augmented document...
}
catch (SAXException ex) {
System.out.println(args[0] + " is not valid because ");
System.out.println(ex.getMessage());
}
}
4) giving error
Test1.xml is not valid because
cvc-elt.1: Cannot find the declaration of element 'd'.
5)here the namespaces refer in XSd in bold are creating problem.if we remove this it will validate it
Can someone help
Thanks
I am using JAva APi for validating XML using xsd
1)XML is
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="spl.xsl"?>
<d>
<document type="test">123</document>
</d>
2) xsd is
<?xml version="1.0" encoding="ASCII"?>
<xs:schema targetNamespace="urn:hl7-org:v3" xmlns:mif="urn:hl7-org:v3/mif" xmlns="urn:hl7-org:v3" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="d">
<xs:complexType>
<xs:sequence>
<xs:element ref="document" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="document">
<xs:complexType mixed="true">
<xs:attribute name="type" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
3)java code
SchemaFactory factory
= SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
File schemaLocation = new File("C:/workspace/unZip/Test1.xsd");
Schema schema = factory.newSchema(schemaLocation);
Validator validator = schema.newValidator();
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File(args[0]));
DOMSource source = new DOMSource(doc);
DOMResult result = new DOMResult();
try {
validator.validate(source, result);
Document augmented = (Document) result.getNode();
// do whatever you need to do with the augmented document...
}
catch (SAXException ex) {
System.out.println(args[0] + " is not valid because ");
System.out.println(ex.getMessage());
}
}
4) giving error
Test1.xml is not valid because
cvc-elt.1: Cannot find the declaration of element 'd'.
5)here the namespaces refer in XSd in bold are creating problem.if we remove this it will validate it
Can someone help
Thanks