In my application, we use XSD validation to check whether the given text input is a number greater or equal to 0. This works fine, but when we enter numbers like 000 or 001 it validates it too. So when we write to a file it writes the 000 and 001 but we want to not validate them. We want them as 0 or 1, not 000 or 001.
How can we enable it and trim the 00 at the beginning? Is there an XSD type or restriction for it?
How can we solve this issue?
I am not sure if you only want to validate the content given to write or read from the files or also alter it. the xml schema cannot alter the data. Assuming the to be validated content is of the type string. You can use a regex pattern to check if the content is correct. the below example gives checks if the last character in the string is 0 or 1 and all the characters prior to it must be 0.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="input">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="(0*[0-1])?[0-1]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
update after receiving comments
if you only want to approve the xml structure that has a single digit in the range of 0 to 9 you can use the below example.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="input">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:pattern value="[0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Related
I am trying to construct an XML document using org.w3c.dom.Document to create some XML for another existing tool. The problem I am having is that the tool seems to have the XML namespaces in a strange way. How can I replicate this using the Java API's?
<ns1:myroot xmlns:ns1="http://foo.com/ns1/">
<ns2:bar xmlns:ns2="http://foo.com/xml/bar/">
<ns2:bar_thing>abc</ns2:bar_thing>
</ns2:bar>
<ns3:data xmlns:ns3="http://foo.com/xml/special-ns">
<!--These are not namespaced for some reason. If I use the ns3 prefix, or
use a default xmls="...", the tool fails to load the document, saying the
elements have invalid values.
-->
<a>Element without namespace</a>
<b>
<bi>1</bi>
<bii>2</bii>
</b>
</ns3:data>
</ns1:myroot>
I can build most of the document easily with createElementNS and setAttributeNS. However I can't get the ns3:data contents to be correct.
Trying to use the non-namespace createElement still left an xmlns="http://foo.com/xml/special-ns"> on my a and b elements, as did using createElementNS with an empty namespace, and obviously a non-empty namespace puts them in a namespace.
The schema for http://foo.com/xml/special-ns has a bunch of declarations like these below, not sure what the tns thing is about, but otherwise does not seem special (although I am not 100% sure the tool actually does anything with the XSD and I don't have access to the source).
<xs:schema version="1.0" targetNamespace="http://foo.com/xml/special-ns">
<!--Bunch of xs:element such as this-->
<xs:element name="data" type="tns:data" />
<!--types declared after-->
<xs:complexType name="data">
<xs:sequence>
<xs:element name="a" type="tns:aDataObj" minOccurs="0"/>
<xs:element name="b" type="tns:bDataObj" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="aDataObj">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9 ]+" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="bDataObj">
<xs:sequence>
<xs:element name="bi" type="xs:string"/>
<xs:element name="bii" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
I want to validate AGE filed in my schema but i have given some restriction on it but when i give the string value in AGE filed which is not throwing the error like "Invalid data for AGE". May i know how to solve this issue? Please find my sample code below.
XML File
<?xml version="1.0"?>
<Employee>
<AGE>thf</AGE>
</Employee>
XSD File
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="AGE">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="65"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
In front of me there is a problem - a complete schema parse (with include, import, redefine). The problem, is not even parsing scheme, it's to resolve types from this schema.
To explain the problem, here is an example:
schema.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:myprefix="http://myhost.ru/service/" xmlns:anprefix="http://anotherhost.ru/pub-service/" targetNamespace="http://myhost.ru/service/">
<xs:import namespace="http://anotherhost.ru/pub-service/" schemaLocation="anotherhost_schema.xsd"/>
<xs:complexType>
<xs:sequence>
<xs:element name="ServiceCode" type="anprefix:ServiceCodeType">
<xs:annotation>
<xs:documentation>Service code</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="MessageClass" type="myprefix:MessageClassType"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="MessageClassType">
<xs:restriction base="xs:string">
<xs:enumeration value="REQUEST">
<xs:annotation>
<xs:documentation>Request from client
</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="RESPONSE">
<xs:annotation>
<xs:documentation>Response to client</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:schema>
anotherhost_schema.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:anprefix="http://anotherhost.ru/pub-service/" targetNamespace="http://anotherhost.ru/pub-service/">
<xs:simpleType name="ServiceCodeType">
<restriction base="xs:string">
</restriction>
</xs:simpleType>
</xs:schema>
Example is not very difficult, the real problem is much bigger. My problem is to parse schema and create it's internal representation (that form generator and request handler will use), for example, as follows:
{
"ServiceCode": {"string", String.class, "Service code"},
"MessageClass": {"string", {"REQUEST":"Request from client","RESPONSE":"Response to client"}, ""}
}
Inner representation can be various, but simple for use in Java. Is there such a library? I know, that there is a library, called JAXB, created specially for XML-parsing, but I don't understand how to bind it to my problem.
My cron.xml:
<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
<cron>
<url>/myURL</url>
<description>Backup data 02 times per day</description>
<schedule>every 12 hours</schedule>
<timezone>America/New_York</timezone>
<target>ah-builtin-python-bundle</target>
</cron>
</cronentries>
When I upload my application to gae, I get an error message:
An internal error occurred during: "Deploying App to Google". XML
error validating
/Users/Aptos/Documents/workspace/App/war/WEB-INF/cron.xml against
/Users/Aptos/appengine-java-sdk-1.7.0/docs/cron.xsd
cron.xsd file:
http://code.google.com/p/googleappengine/source/browse/trunk/java/docs/cron.xsd?r=109
Solution : If the url element contains the special XML characters &, <, >, ', or ", you should escape them.
Thank you very much
You have an extra tag :target (the last tag), this tag does not appear in the XSD.
At least in the file you link to there is no target element in the cron element.
It looks like you have an old xsd file the latest xsd look like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="cronentries" type="cronentries-Type"/>
<xs:complexType name="cronentries-Type">
<xs:sequence>
<xs:element type="cron-Type" name="cron" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="cron-Type">
<xs:all>
<xs:element type="xs:string" name="url"/>
<xs:element type="xs:string" name="description" minOccurs="0"/>
<xs:element type="xs:string" name="schedule"/>
<xs:element type="xs:string" name="timezone" minOccurs="0"/>
<xs:element type="target-Type" name="target" minOccurs="0"/>
</xs:all>
</xs:complexType>
<xs:simpleType name="target-Type">
<xs:restriction base="xs:string">
<xs:pattern value="[a-z\d\-]{1,100}"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
The XSDs of these AppEngine configuration files are "namespaceless" (i.e. no targetNamespace compare with appengine-web.xsd's xs:schema), so you have to declare it as such:
<?xml version="1.0" encoding="UTF-8"?>
<cronentries xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://googleappengine.googlecode.com/svn-history/r109/trunk/java/docs/cron.xsd">
<cron>
<url>/myURL</url>
<description>Backup data 02 times per day</description>
<schedule>every 12 hours</schedule>
<timezone>America/New_York</timezone>
<target>ah-builtin-python-bundle</target>
</cron>
</cronentries>
Also note that you have to use the raw version of the file:
https://googleappengine.googlecode.com/svn-history/r109/trunk/java/docs/cron.xsd
instead of the browser version:
https://code.google.com/p/googleappengine/source/browse/trunk/java/docs/cron.xsd?r=109
I've got something like the following XML file.
<credits>
<property name="tag">
<item>v0003</item>
</property>
<property
name="tag">
<item>mhma03h</item>
</property>
</credits>
First requirement is that this XML cannot change no matter what. Please don't suggest doing that below.
I need to write a schema that validates this. And Java code that does the validation.
I am completely stuck and I have no idea really what is going on.
What's a schema like this look like? I've gotten one but it's so bad I'm not going to bother posting. :P I don't want to have to append a namespace to the XML elements. They're set in stone.^_^
How do I just make it so all of these elements are just "found" by the parser? Can I just tell it ignore all this namespace nonsense. With this application of validating against a schema, namespace conflicts are simply impossible.
I've tried putting
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="books" elementFormDefault="unqualified">
for my namespace info and
UPDATE: I've updated what I'm doing to reflect the answers given so far! :)
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="property">
<xs:complexType>
<xs:sequence>
<xs:element ref="item"/>
</xs:sequence>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="tag"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="mhma03h"/>
<xs:enumeration value="v0003"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="credits">
<xs:complexType>
<xs:sequence>
<xs:element ref="property" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML
v0003
Code to load and validate yes. Before you ask, the files are able to be loaded. I've checked like 20 times. :P
SAXParserFactory factory = SAXParserFactory.newInstance();
SchemaFactory schemaFactory =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
factory.setSchema(schemaFactory.newSchema(
new Source[] {new StreamSource("small.xsd")}));
javax.xml.parsers.SAXParser parser = factory.newSAXParser();
org.xml.sax.XMLReader reader = parser.getXMLReader();
reader.setFeature("http://xml.org/sax/features/validation", true);
reader.setFeature("http://apache.org/xml/features/validation/schema", true);
reader.parse(new InputSource("small.xml"));
Here is a schema that corresponds to the XML file you posted:
<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSpy v2011 sp1 (http://www.altova.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="property">
<xs:complexType>
<xs:sequence>
<xs:element ref="item"/>
</xs:sequence>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="tag"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="mhma03h"/>
<xs:enumeration value="v0003"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="credits">
<xs:complexType>
<xs:sequence>
<xs:element ref="property" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This may not correctly capture the requirements for each element and attribute (check that the min/max occurs, required/optional proprerties, etc... are set correctly) but it should get you started on working with an XML schema that will validate correctly. The schema does not define a target namespace so you won't have to worry about modifying the existing XML to add in namespace prefixes to your existing elements.
You need to find a way to tell the XML processor to use your namespace as the default when it is processing a document without an explicit namespace. Most processors have a way to do this, IIRC there is a method named setNoNamespaceSchema or something similar. You would write an XML schema with a namespace and tell the processor (validator, whatever) to use your namespace for documents that do not have an explicit namespace.