How to call an external xsd in an xml file?

kratos19995

New Member
Hi all this is my xml file with xsd defined as internal to it\[code\]<?xml version="1.0"?><catalog xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:x="urn:book"> <!-- START OF SCHEMA --><xsd:schema targetNamespace="urn:book"> <xsd:element name="book"> <xsd:complexType> <xsd:sequence> <xsd:element name="author" type="xsd:string"/> <xsd:element name="title" type="xsd:string"/> <xsd:element name="genre" type="xsd:string"/> <xsd:element name="price" type="xsd:float"/> <xsd:element name="publish_date" type="xsd:date"/> <xsd:element name="description" type="xsd:string"/> <xsd:element name="number" type="xsd:integer"/> </xsd:sequence> <xsd:attribute name="id" type="xsd:string"/> </xsd:complexType> </xsd:element></xsd:schema><!-- END OF SCHEMA --> <x:book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> <number>123.4 </number> </x:book></catalog>\[/code\]i want to validate it in java and i have written below code for java and it is working perfectly for this xml.\[code\]import java.io.*;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.Source;import javax.xml.transform.dom.DOMSource;import javax.xml.validation.*;import org.w3c.dom.NodeList;import org.w3c.dom.Node;import org.xml.sax.SAXException;import javax.xml.xpath.*;import org.xml.sax.InputSource;public class TestValidation { public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException { XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList)xpath.evaluate("/*/*", new InputSource("E:\\abc.xml"), XPathConstants.NODESET); SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Validator validator = factory.newSchema(new DOMSource(nodes.item(0))).newValidator(); try { validator.validate(new DOMSource(nodes.item(1))); System.out.println("XML is valid."); } catch (SAXException ex) { System.out.println("XML is not valid because " + ex.getMessage()); } }}\[/code\]Now i want to define xsd as a seprate external file,i know how i can define that,can any one help me to find how to call that xsd from this xml and then do i need to make any changes in my java program
 
Back
Top