You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Ju...@mincom.com on 2007/04/04 03:33:25 UTC

Validating XML documents against schemas

Hi.

I am receiving XML documents from a number of different companies.
The documents I receive can contain a variety of different schema/dtd 
declarations
eg 
<!DOCTYPE OrderResponse SYSTEM "XCBL30.dtd">
<?soxtype 
urn:x-commerceone:document:com:commerceone:XCBL30:XCBL30.sox$1.0?>
<Order xmlns="rrn:org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="rrn:org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd Order.xsd">
or even no declaration at all.

I want to validate the documents that I receive against a xsd schema, 
disregarding the declaration contained in the document.

I thought that I would be able to achieve this by writing my own 
EntityResolver, and always resolve to a shema, however I have found that 
this doesn't work.
It only works when a xsd schema declaration is passed.

For both sox schema declarations and no declaration I get : 
ERROR-cvc-elt.1: Cannot find the declaration of element 'OrderResponse'. 
before the resolveEntity method of the Entity Resolver is even called.

For dtd declaration I get : SAXException - The markup declarations 
contained or pointed to by the document type declaration must be 
well-formed. after the EntityResolver is called.

I am using the org.apache.xerces.parsers.SAXParser to perform the 
validation.

         SAXParser parser = new SAXParser(); 
 
parser.setProperty("http://apache.org/xml/properties/internal/grammar-pool", 
grammarCache); 
         parser.setFeature("http://xml.org/sax/features/validation", 
true); 
 parser.setFeature("http://apache.org/xml/features/validation/schema", 
true);
 
         SchemaEntityResolver etyRes = new SchemaEntityResolver();
         etyRes.setSchemaLocation(the_location);
         etyRes.setFormat("XCBL");
         etyRes.setVersion("30");
         etyRes.setSchemaName(schemaName);  // just sets the name of the 
schema to use

         ValidationHandler handler = new ValidationHandler();
         parser.setErrorHandler(handler);

         parser.setEntityResolver(etyRes);

         StringReader stringReader = new StringReader(the_document);
         InputSource inSource = new InputSource(stringReader);
         parser.parse(inSource);

Does anyone know of any way I can force my document to be validated 
against a schema.
Or do I have to manipulate the received document to remove the passed 
declarations, and insert my own.

Thanks

-- 
This transmission is for the intended addressee only and is confidential information. If you have received this transmission in error, please notify the sender and delete the transmission. The contents of this e-mail are the opinion of the writer only and are not endorsed by the Mincom Group of companies unless expressly stated otherwise.
-- 






Re: Validating XML documents against schemas

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Julie,

If the target namespace of your schema document is "
rrn:org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd" then the documents you have are
missing a namespace declaration for that namespace and thus aren't valid
against the schema in their current state.

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

Julie.Hansen@mincom.com wrote on 10/23/2007 01:45:04 AM:

> Hi Michael
>
> Thanks for your response.
> I was pulled to another urgent project, so unfortunately had to
> leave this unfinished.
> However I am back now, and am keen to have another crack at getting
> this working.
>
> As stated previously my requirement is as follows :
> > I am receiving XML documents from a number of different companies.
> > The documents I receive can contain a variety of different
> > schema/dtd declarations
> > eg
> > <!DOCTYPE OrderResponse SYSTEM "XCBL30.dtd">
> > <?soxtype
> urn:x-commerceone:document:com:commerceone:XCBL30:XCBL30.sox$1.0?>
> > <Order xmlns="rrn:org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd" xmlns:xsi="
> > http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="rrn:
> > org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd Order.xsd">
> > or even no declaration at all.
> >
> > I want to validate the documents that I receive against a xsd
> > schema, disregarding the declaration contained in the document.
> >
>
>
>
> I am having some problems implementing the jaxp solution you suggested.
>
> I have downloaded the latest xerces-2_9_0, and jaxp 1.3.
> I am using the sample SourceValidator class, to do as you suggested.
>
>
> However I am finding that the validator does not seem to be using
> the schema alone to validate all my documents.
>
> eg.
> I have tried validating the following documents :
>
> <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE OrderResponse SYSTEM
> "XCBL30.dtd"><Order>
>
<OrderHeader><OrderNumber><BuyerOrderNumber>00000004</BuyerOrderNumber></OrderNumber><OrderIssueDate>20070323T09:

> 55:10+10:00</OrderIssueDate><OrderReferences><AccountCode> ...snipped
>
> error: Parse error occurred - D:
> \Eclipse\Workspaces\xerces-2_9_0\xerces-2_9_0\test\XCBL30.dtd (The
> system cannot find the file specified)
>
>
> <?xml version="1.0" encoding="UTF-8"?><Order>
>
<OrderHeader><OrderNumber><BuyerOrderNumber>00000004</BuyerOrderNumber></OrderNumber><OrderIssueDate>20070323T09:

> 55:10+10:00</OrderIssueDate>
> <OrderReferences><AccountCode>...............snipped
>
> [Error] NONE_301889302696_Order.txt:2:8: cvc-elt.1: Cannot find the
> declaration of element 'Order'.
>
> <?xml version="1.0"?><?soxtype urn:x-commerceone:document:com:
> commerceone:XCBL30:XCBL30.sox$1.0?><Order>
>
<OrderHeader><OrderNumber><BuyerOrderNumber>00000004</BuyerOrderNumber></OrderNumber><OrderIssueDate>20070323T09:

> 55:10+10:00</OrderIssueDate><OrderReferences><AccountCode>
>
> [Error] SOX_301889302696_Order.txt:2:8: cvc-elt.1: Cannot find the
> declaration of element 'Order'.
>
> <?xml version="1.0" encoding="UTF-8"?>
> <Order xmlns="rrn:org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd" xmlns:xsi="
> http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="rrn:
> org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd Order.xsd">
>
<OrderHeader><OrderNumber><BuyerOrderNumber>00000004</BuyerOrderNumber></OrderNumber><OrderIssueDate>20070323T09:

> 55:10+10:00</OrderIssueDate><OrderReferences><AccountCode>
> This document is the only one that actually performs the validation.
> I am not sure if I am barking up the wrong tree with what I am
> trying to do, or whether I just need to manipulate some features or
> properties of the validator
> ANy feedback you could give would be greatly appreciated.
> Thanks
> Julie
>
>

>
> Michael Glavassevich <mr...@ca.ibm.com>
> 05/04/2007 02:24 PM
>
> Please respond to
> j-users@xerces.apache.org
>
> To
>
> j-users@xerces.apache.org
>
> cc
>
> Julie.Hansen@mincom.com
>
> Subject
>
> Re: Validating XML documents against schemas
>
>
>
>
> Hi Julie,
>
> Not sure what's wrong here (since you've left out some details), but you
> may find it easier to get this working using the JAXP 1.3 Validation API.

> You can compile your schema using the SchemaFactory [1] and then use the
> Schema [2] object it returns for validation. Schema location hints
> specified in your instance document will be ignored. There are samples
> [3][4] included in the binary distribution which show how to do this.
>
> Thanks.
>
> [1]
> http://xerces.apache.org/xerces2-
> j/javadocs/api/javax/xml/validation/SchemaFactory.html
> [2]
> http://xerces.apache.org/xerces2-
> j/javadocs/api/javax/xml/validation/Schema.html
> [3] http://xerces.apache.org/xerces2-j/samples-jaxp.html#SourceValidator
> [4] http://xerces.apache.org/xerces2-j/samples-jaxp.html#ParserAPIUsage
>
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: mrglavas@ca.ibm.com
> E-mail: mrglavas@apache.org
>
> Julie.Hansen@mincom.com wrote on 04/03/2007 09:33:25 PM:
>
> > Hi.
> >
> > I am receiving XML documents from a number of different companies.
> > The documents I receive can contain a variety of different
> > schema/dtd declarations
> > eg
> > <!DOCTYPE OrderResponse SYSTEM "XCBL30.dtd">
> > <?soxtype
> urn:x-commerceone:document:com:commerceone:XCBL30:XCBL30.sox$1.0?>
> > <Order xmlns="rrn:org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd" xmlns:xsi="
> > http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="rrn:
> > org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd Order.xsd">
> > or even no declaration at all.
> >
> > I want to validate the documents that I receive against a xsd
> > schema, disregarding the declaration contained in the document.
> >
> > I thought that I would be able to achieve this by writing my own
> > EntityResolver, and always resolve to a shema, however I have found
> > that this doesn't work.
> > It only works when a xsd schema declaration is passed.
> >
> > For both sox schema declarations and no declaration I get : ERROR-
> > cvc-elt.1: Cannot find the declaration of element 'OrderResponse'.
> > before the resolveEntity method of the Entity Resolver is even called.
> >
> > For dtd declaration I get : SAXException - The markup declarations
> > contained or pointed to by the document type declaration must be
> well-formed.
> > after the EntityResolver is called.
> >
> > I am using the org.apache.xerces.parsers.SAXParser to perform the
> validation.
> >
> >          SAXParser parser = new SAXParser();
> >          parser.setProperty("http://apache.
> > org/xml/properties/internal/grammar-pool", grammarCache);
> >          parser.setFeature("http://xml.org/sax/features/validation",
> true);
> >          parser.setFeature("
> http://apache.org/xml/features/validation/schema
> > ", true);
> >
> >          SchemaEntityResolver etyRes = new SchemaEntityResolver();
> >          etyRes.setSchemaLocation(the_location);
> >          etyRes.setFormat("XCBL");
> >          etyRes.setVersion("30");
> >          etyRes.setSchemaName(schemaName);  // just sets the name of
> > the schema to use
> >
> >          ValidationHandler handler = new ValidationHandler();
> >          parser.setErrorHandler(handler);
> >
> >          parser.setEntityResolver(etyRes);
> >
> >          StringReader stringReader = new StringReader(the_document);
> >          InputSource inSource = new InputSource(stringReader);
> >          parser.parse(inSource);
> >
> > Does anyone know of any way I can force my document to be validated
> > against a schema.
> > Or do I have to manipulate the received document to remove the
> > passed declarations, and insert my own.
> > Thanks
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Validating XML documents against schemas

Posted by Ju...@mincom.com.
Hi Michael

Thanks for your response.
I was pulled to another urgent project, so unfortunately had to leave this 
unfinished.
However I am back now, and am keen to have another crack at getting this 
working.

As stated previously my requirement is as follows :
> I am receiving XML documents from a number of different companies. 
> The documents I receive can contain a variety of different 
> schema/dtd declarations 
> eg 
> <!DOCTYPE OrderResponse SYSTEM "XCBL30.dtd"> 
> <?soxtype 
urn:x-commerceone:document:com:commerceone:XCBL30:XCBL30.sox$1.0?> 
> <Order xmlns="rrn:org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd" xmlns:xsi="
> http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="rrn:
> org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd Order.xsd"> 
> or even no declaration at all. 
> 
> I want to validate the documents that I receive against a xsd 
> schema, disregarding the declaration contained in the document. 
> 



I am having some problems implementing the jaxp solution you suggested.

I have downloaded the latest xerces-2_9_0, and jaxp 1.3.
I am using the sample SourceValidator class, to do as you suggested.


However I am finding that the validator does not seem to be using the 
schema alone to validate all my documents.

eg.
I have tried validating the following documents :

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE OrderResponse SYSTEM 
"XCBL30.dtd"><Order> 
<OrderHeader><OrderNumber><BuyerOrderNumber>00000004</BuyerOrderNumber></OrderNumber><OrderIssueDate>20070323T09:55:10+10:00</OrderIssueDate><OrderReferences><AccountCode> 
...snipped

error: Parse error occurred - 
D:\Eclipse\Workspaces\xerces-2_9_0\xerces-2_9_0\test\XCBL30.dtd (The 
system cannot find the file specified)


<?xml version="1.0" encoding="UTF-8"?><Order> 
<OrderHeader><OrderNumber><BuyerOrderNumber>00000004</BuyerOrderNumber></OrderNumber><OrderIssueDate>20070323T09:55:10+10:00</OrderIssueDate>
<OrderReferences><AccountCode>...............snipped

[Error] NONE_301889302696_Order.txt:2:8: cvc-elt.1: Cannot find the 
declaration of element 'Order'.

<?xml version="1.0"?><?soxtype 
urn:x-commerceone:document:com:commerceone:XCBL30:XCBL30.sox$1.0?><Order> 
<OrderHeader><OrderNumber><BuyerOrderNumber>00000004</BuyerOrderNumber></OrderNumber><OrderIssueDate>20070323T09:55:10+10:00</OrderIssueDate><OrderReferences><AccountCode>

[Error] SOX_301889302696_Order.txt:2:8: cvc-elt.1: Cannot find the 
declaration of element 'Order'.

<?xml version="1.0" encoding="UTF-8"?>
<Order xmlns="rrn:org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="rrn:org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd Order.xsd"> 
<OrderHeader><OrderNumber><BuyerOrderNumber>00000004</BuyerOrderNumber></OrderNumber><OrderIssueDate>20070323T09:55:10+10:00</OrderIssueDate><OrderReferences><AccountCode>
This document is the only one that actually performs the validation.

I am not sure if I am barking up the wrong tree with what I am trying to 
do, or whether I just need to manipulate some features or properties of 
the validator

ANy feedback you could give would be greatly appreciated.
Thanks
Julie






Michael Glavassevich <mr...@ca.ibm.com> 
05/04/2007 02:24 PM
Please respond to
j-users@xerces.apache.org


To
j-users@xerces.apache.org
cc
Julie.Hansen@mincom.com
Subject
Re: Validating XML documents against schemas






Hi Julie,

Not sure what's wrong here (since you've left out some details), but you 
may find it easier to get this working using the JAXP 1.3 Validation API. 
You can compile your schema using the SchemaFactory [1] and then use the 
Schema [2] object it returns for validation. Schema location hints 
specified in your instance document will be ignored. There are samples 
[3][4] included in the binary distribution which show how to do this.

Thanks.

[1] 
http://xerces.apache.org/xerces2-j/javadocs/api/javax/xml/validation/SchemaFactory.html

[2] 
http://xerces.apache.org/xerces2-j/javadocs/api/javax/xml/validation/Schema.html

[3] http://xerces.apache.org/xerces2-j/samples-jaxp.html#SourceValidator
[4] http://xerces.apache.org/xerces2-j/samples-jaxp.html#ParserAPIUsage

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

Julie.Hansen@mincom.com wrote on 04/03/2007 09:33:25 PM:

> Hi. 
> 
> I am receiving XML documents from a number of different companies. 
> The documents I receive can contain a variety of different 
> schema/dtd declarations 
> eg 
> <!DOCTYPE OrderResponse SYSTEM "XCBL30.dtd"> 
> <?soxtype 
urn:x-commerceone:document:com:commerceone:XCBL30:XCBL30.sox$1.0?> 
> <Order xmlns="rrn:org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd" xmlns:xsi="
> http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="rrn:
> org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd Order.xsd"> 
> or even no declaration at all. 
> 
> I want to validate the documents that I receive against a xsd 
> schema, disregarding the declaration contained in the document. 
> 
> I thought that I would be able to achieve this by writing my own 
> EntityResolver, and always resolve to a shema, however I have found 
> that this doesn't work. 
> It only works when a xsd schema declaration is passed. 
> 
> For both sox schema declarations and no declaration I get : ERROR-
> cvc-elt.1: Cannot find the declaration of element 'OrderResponse'. 
> before the resolveEntity method of the Entity Resolver is even called. 
> 
> For dtd declaration I get : SAXException - The markup declarations 
> contained or pointed to by the document type declaration must be 
well-formed. 
> after the EntityResolver is called. 
> 
> I am using the org.apache.xerces.parsers.SAXParser to perform the 
validation.
> 
>          SAXParser parser = new SAXParser(); 
>          parser.setProperty("http://apache.
> org/xml/properties/internal/grammar-pool", grammarCache); 
>          parser.setFeature("http://xml.org/sax/features/validation", 
true); 
>          parser.setFeature("
http://apache.org/xml/features/validation/schema
> ", true); 
> 
>          SchemaEntityResolver etyRes = new SchemaEntityResolver(); 
>          etyRes.setSchemaLocation(the_location); 
>          etyRes.setFormat("XCBL"); 
>          etyRes.setVersion("30"); 
>          etyRes.setSchemaName(schemaName);  // just sets the name of
> the schema to use 
> 
>          ValidationHandler handler = new ValidationHandler(); 
>          parser.setErrorHandler(handler); 
> 
>          parser.setEntityResolver(etyRes); 
> 
>          StringReader stringReader = new StringReader(the_document); 
>          InputSource inSource = new InputSource(stringReader); 
>          parser.parse(inSource);
> 
> Does anyone know of any way I can force my document to be validated 
> against a schema. 
> Or do I have to manipulate the received document to remove the 
> passed declarations, and insert my own. 
> Thanks 

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org




-- 
This transmission is for the intended addressee only and is confidential information. If you have received this transmission in error, please notify the sender and delete the transmission. The contents of this e-mail are the opinion of the writer only and are not endorsed by the Mincom Group of companies unless expressly stated otherwise.
-- 






Re: Validating XML documents against schemas

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Julie,

Not sure what's wrong here (since you've left out some details), but you 
may find it easier to get this working using the JAXP 1.3 Validation API. 
You can compile your schema using the SchemaFactory [1] and then use the 
Schema [2] object it returns for validation. Schema location hints 
specified in your instance document will be ignored. There are samples 
[3][4] included in the binary distribution which show how to do this.

Thanks.

[1] 
http://xerces.apache.org/xerces2-j/javadocs/api/javax/xml/validation/SchemaFactory.html
[2] 
http://xerces.apache.org/xerces2-j/javadocs/api/javax/xml/validation/Schema.html
[3] http://xerces.apache.org/xerces2-j/samples-jaxp.html#SourceValidator
[4] http://xerces.apache.org/xerces2-j/samples-jaxp.html#ParserAPIUsage

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

Julie.Hansen@mincom.com wrote on 04/03/2007 09:33:25 PM:

> Hi. 
> 
> I am receiving XML documents from a number of different companies. 
> The documents I receive can contain a variety of different 
> schema/dtd declarations 
> eg 
> <!DOCTYPE OrderResponse SYSTEM "XCBL30.dtd"> 
> <?soxtype 
urn:x-commerceone:document:com:commerceone:XCBL30:XCBL30.sox$1.0?> 
> <Order xmlns="rrn:org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd" xmlns:xsi="
> http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="rrn:
> org.xcbl:schemas/xcbl/v3_0/xcbl30.xsd Order.xsd"> 
> or even no declaration at all. 
> 
> I want to validate the documents that I receive against a xsd 
> schema, disregarding the declaration contained in the document. 
> 
> I thought that I would be able to achieve this by writing my own 
> EntityResolver, and always resolve to a shema, however I have found 
> that this doesn't work. 
> It only works when a xsd schema declaration is passed. 
> 
> For both sox schema declarations and no declaration I get : ERROR-
> cvc-elt.1: Cannot find the declaration of element 'OrderResponse'. 
> before the resolveEntity method of the Entity Resolver is even called. 
> 
> For dtd declaration I get : SAXException - The markup declarations 
> contained or pointed to by the document type declaration must be 
well-formed. 
> after the EntityResolver is called. 
> 
> I am using the org.apache.xerces.parsers.SAXParser to perform the 
validation.
> 
>          SAXParser parser = new SAXParser(); 
>          parser.setProperty("http://apache.
> org/xml/properties/internal/grammar-pool", grammarCache); 
>          parser.setFeature("http://xml.org/sax/features/validation", 
true); 
>          parser.setFeature("
http://apache.org/xml/features/validation/schema
> ", true); 
> 
>          SchemaEntityResolver etyRes = new SchemaEntityResolver(); 
>          etyRes.setSchemaLocation(the_location); 
>          etyRes.setFormat("XCBL"); 
>          etyRes.setVersion("30"); 
>          etyRes.setSchemaName(schemaName);  // just sets the name of
> the schema to use 
> 
>          ValidationHandler handler = new ValidationHandler(); 
>          parser.setErrorHandler(handler); 
> 
>          parser.setEntityResolver(etyRes); 
> 
>          StringReader stringReader = new StringReader(the_document); 
>          InputSource inSource = new InputSource(stringReader); 
>          parser.parse(inSource);
> 
> Does anyone know of any way I can force my document to be validated 
> against a schema. 
> Or do I have to manipulate the received document to remove the 
> passed declarations, and insert my own. 
> Thanks 

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org