You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Vance Vagell <va...@vancevagell.com> on 2007/05/02 23:18:26 UTC
Validating a value against a SchemaType object
Hi folks,
First, thanks for the tip earlier about atomic type validation, Cezar. Here is another issue I've been working on, that I hope someone has insight into. It is similar to this old thread:
http://mail-archives.apache.org/mod_mbox/xmlbeans-user/200501.mbox/%3c4B2B4C417991364996F035E1EE39E2E124FC35@uskiex01.amer.bea.com%3e
I have a SchemaType object that I've loaded from an .xsd file. I also have a piece of data that I'd like to validate against that SchemaType object. Here is how I'm trying to do it:
String value = "car"; // The value I want to validate.
XmlObject xmlObject = XmlObject.Factory.parse("<product>" + value + "</product>"); // I make a temporary XmlObject to hold the value.
xmlObject = xmlObject.selectChildren(new QName("product"))[0]; // I get the <product> element (what I want to validate), not the doc root.
xmlObject = xmlObject.changeType(schemaType); // I specify the type to validate against.
return xmlObject.validate(); // I perform validation. This always returns false...
And here is the schema snippet that defines the type that is held in schemaType:
<xsd:simpleType name="productType">
<xsd:restriction base='xsd:string'>
<xsd:enumeration value='truck'/>
<xsd:enumeration value='car'/>
</xsd:restriction>
</xsd:simpleType>
I get an error (not an exception) when xmlObject.validate() is called; I used XmlOptions.setErrorListener() to get the error info. Here is the error:
Message: Invalid type
Location of invalid XML: <product>car</product>
Does the mailer thread I linked earlier mean that I can't validate programatically this way? The types I'm dealing with are completely unknown before runtime, as these are end-user-specified schemas and values. Compiling these into Java classes isn't an option for this application.
If the way I'm doing it is wrong, is there another way?
Thanks for the help,
Vance
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org
Re: Validating a value against a SchemaType object
Posted by Jacob Danner <ja...@gmail.com>.
Hi Vance,
There are some scripts that you can use to validate an instance to a
schema type. Take a peek at the svalidate/validate scripts in the bin
directory.
The class that is called is:
org.apache.xmlbeans.impl.tool.InstanceValidator
and might do what you are looking for.
Hope this helps,
-Jacob Danner
On 5/2/07, Vance Vagell <va...@vancevagell.com> wrote:
> Hi folks,
>
> First, thanks for the tip earlier about atomic type validation, Cezar. Here is another issue I've been working on, that I hope someone has insight into. It is similar to this old thread:
>
> http://mail-archives.apache.org/mod_mbox/xmlbeans-user/200501.mbox/%3c4B2B4C417991364996F035E1EE39E2E124FC35@uskiex01.amer.bea.com%3e
>
> I have a SchemaType object that I've loaded from an .xsd file. I also have a piece of data that I'd like to validate against that SchemaType object. Here is how I'm trying to do it:
>
> String value = "car"; // The value I want to validate.
> XmlObject xmlObject = XmlObject.Factory.parse("<product>" + value + "</product>"); // I make a temporary XmlObject to hold the value.
> xmlObject = xmlObject.selectChildren(new QName("product"))[0]; // I get the <product> element (what I want to validate), not the doc root.
> xmlObject = xmlObject.changeType(schemaType); // I specify the type to validate against.
> return xmlObject.validate(); // I perform validation. This always returns false...
>
> And here is the schema snippet that defines the type that is held in schemaType:
>
> <xsd:simpleType name="productType">
> <xsd:restriction base='xsd:string'>
> <xsd:enumeration value='truck'/>
> <xsd:enumeration value='car'/>
> </xsd:restriction>
> </xsd:simpleType>
>
> I get an error (not an exception) when xmlObject.validate() is called; I used XmlOptions.setErrorListener() to get the error info. Here is the error:
>
> Message: Invalid type
> Location of invalid XML: <product>car</product>
>
> Does the mailer thread I linked earlier mean that I can't validate programatically this way? The types I'm dealing with are completely unknown before runtime, as these are end-user-specified schemas and values. Compiling these into Java classes isn't an option for this application.
>
> If the way I'm doing it is wrong, is there another way?
>
> Thanks for the help,
> Vance
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org