Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

XML Validate with XSD Schema included

843834Apr 1 2009 — edited Apr 2 2009
Hello evebody, I would like to know if anybody have alreary had the same problem.
I have a XML file that aro going to be validated from two XSD files using include tag because one of than uses elements from the other one.
So, when I´m trying to run it, an exception is launched, but if I build only onr XSD file with all the elements it works fine.
I´ll show you my code.
Thank´s if anybody can help.
Pessoa.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	xmlns:obj="http://domain.com/xsd" 
	targetNamespace="http://domain.com/xsd" 
	elementFormDefault="qualified">
	
	<xs:complexType name="Pessoa">
		<xs:sequence>        
			<xs:element name="nome" type="xs:string"/>
			<xs:element name="telefone" type="xs:string"/>
		</xs:sequence>
	</xs:complexType>
	
</xs:schema>

PessoaFisica.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	xmlns:obj="http://domain.com/xsd" 
	targetNamespace="http://domain.com/xsd"
	elementFormDefault="qualified">
	
	<xs:include schemaLocation="Pessoa.xsd" />
	
	<xs:complexType name="PessoaFisica">
		<xs:complexContent>
			<xs:extension base="obj:Pessoa">
				<xs:sequence>        
					<xs:element name="cpf" type="xs:string"/>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	
</xs:schema>

Simple.xml
<?xml version="1.0" encoding="UTF-8"?>
<xsd:PessoaFisica 
	xsi:type="xsd:PessoaFisica"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:xsd="http://domain.com/xsd">
	<xsd:nome>Gabriel Oliveira</xsd:nome>
	<xsd:telefone>12345678</xsd:telefone>
	<xsd:cpf>12345678900</xsd:cpf>
</xsd:PessoaFisica>

The validation Test on Method Class
@Test
	public void testeSimples() throws SAXException, IOException {
		InputStream xml = TesteValidacao.class.getResourceAsStream("/Simple.xml");
		InputStream xsd1 = TesteValidacao.class.getResourceAsStream("/Pessoa.xsd");
		InputStream xsd2 = TesteValidacao.class.getResourceAsStream("/PessoaFisica.xsd");
		SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		StreamSource src1 = new StreamSource(xsd1);
		StreamSource src2 = new StreamSource(xsd2);
		Source[] sources = new Source[] {src1, src2};
		Schema schema = factory.newSchema(sources);
		Validator v = schema.newValidator();
		v.validate(new StreamSource(xml));
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 30 2009
Added on Apr 1 2009
5 comments
433 views