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
Related
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>
XML
<ns2:Response xmlns:ns2="http://test.com/" Id="122212">
<Infos size="1">
<Info>
<name>test</name>
</Info>
</Infos>
</ns2:Response>
Generated XSD
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<!-- XML Schema Generated from XML Document on Mon Feb 20 2017 23:20:03 GMT+0530 (India Standard Time) -->
<!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
<xs:element name="ns2:Response">
<xs:complexType>
<xs:sequence>
<xs:element name="Infos">
<xs:complexType>
<xs:sequence>
<xs:element name="Info">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="size" type="xs:int"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="xmlns:ns2" type="xs:string"></xs:attribute>
<xs:attribute name="Id" type="xs:int"></xs:attribute>
</xs:complexType>
</xs:element>
Error
SAX Exception: s4s-att-invalid-value: Invalid attribute value for
'name' in element 'element'. Recorded reason:
cvc-datatype-valid.1.2.1: 'ns2:Response' is not a valid value for
'NCName'.
There are several changes required for your XML and XSD, including:
Change <xs:element name="ns2:Response"> to <xs:element
name="Response"> because element name declarations must be
non-colonized names (NCNAMEs).
Delete <xs:attribute name="xmlns:ns2".../> because namespaces
cannot be declared as attributes.
Add a targetNamespace to the XSD that matches the namespace of the root element in the XML document.
Import a separate XSD for those elements that you wish to be in no namespace (given that your root elements is in a namespace). You must use a separate XSD to accomplish this.
Altogether, your XML,
<?xml version="1.0" encoding="UTF-8"?>
<ns2:Response xmlns:ns2="http://test.com/"
Id="122212">
<Infos size="1">
<Info>
<name>test</name>
</Info>
</Infos>
</ns2:Response>
will validate successfully against these XSD:
Main
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:ns="http://test.com/"
targetNamespace="http://test.com/">
<xs:import schemaLocation="Infos.xsd"/>
<xs:element name="Response">
<xs:complexType>
<xs:sequence>
<xs:element ref="Infos"/>
</xs:sequence>
<xs:attribute name="Id" type="xs:int"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
Imported (Infos.xsd)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Infos">
<xs:complexType>
<xs:sequence>
<xs:element name="Info">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="size" type="xs:int"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
I was able to get a parsed file by, as the above answer suggests, adding a target namepsace to the schema and by adding the ns prefix to all elements.
<?xml version="1.0" encoding="UTF-8"?>
<ns2:Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://test.com testq1.xsd" xmlns:ns2="http://test.com" Id="122212">
<ns2:Infos size="1">
<ns2:Info>
<ns2:name>test</ns2:name>
</ns2:Info>
</ns2:Infos>
and the schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://test.com"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="Response">
<xs:complexType>
<xs:sequence>
<xs:element name="Infos">
<xs:complexType>
<xs:sequence>
<xs:element name="Info">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="size" type="xs:int"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Id" type="xs:int"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
You can approach this in 2 ways.
Approach 1 - multiple schemas
The more typical approach is to have a schema one for each namespace used.
So you end up with schemas that look like this
SampleXml0.xsd
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2017 (https://www.liquid-technologies.com) -->
<xs:schema xmlns:ns2="http://test.com/" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://test.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="C:\Temp\StackOverflow\42351409\SampleXml1.xsd" />
<xs:element name="Response">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="Infos" />
</xs:sequence>
<xs:attribute name="Id" type="xs:unsignedInt" use="optional" />
</xs:complexType>
</xs:element>
</xs:schema>
SampleXml1.xsd
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2017 (https://www.liquid-technologies.com) -->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Infos">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="Info">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="size" type="xs:unsignedByte" use="optional" />
</xs:complexType>
</xs:element>
</xs:schema>
Approach 2 - changing elementFormDefault
Now this is quite specific to your example, but as its only the root element that is qualified with a namespace its possible to change elementFormDefault to unqualified. This has the effect of forcing the elements defined as root element in the schema to have a namespace qualification, while the other elements do not.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2017 (https://www.liquid-technologies.com)-->
<xs:schema xmlns:ns2="http://test.com/" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://test.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Response">
<xs:complexType>
<xs:sequence>
<xs:element name="Infos" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Info" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="size" type="xs:unsignedByte" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Id" type="xs:unsignedInt" use="optional" />
</xs:complexType>
</xs:element>
</xs:schema>
I would probably recommend creating multiple schemas as elementformdefault is typically overlooked by client implementations and ignored.
Why three <xs:sequence> elements? do you really need that? Could you explain better how your code is organized? Usually it is pretty simple, like this:
<xsd:element name="ns2:Response">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xs:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
My webservice should be able to validate xml-input against a schema(request.xsd). The complextyped only element in the schema owns different subelements of types which are defined in other xsds. I am importing all thess xsds. At the end however element type cannot be resolved.
this ist my "request.xsd":
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fe="http://www.some.dir/featurenamespace"
xmlns="http://this.is.my.space"
targetNamespace="http://this.is.my.space"
elementFormDefault="qualified">
<xs:import schemaLocation=" http://www.some.dir/featurenamespace/cmplxtypes.xsd "
namespace=" http://www.some.dir/featurenamespace"/>
<xs:element name="elem1" type="request"/>
<xs:complexType name="request">
<xs:complexContent>
<xs:extension base="fe:complexElement">
<xs:sequence>
<xs:element name="record" type="fe:complexElement "/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
This is the schema (e.g. cmplxtypes.xsd) where the type 'complexElement' is defined:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fe="http://www.some.dir/featurenamespace"
xmlns:ofe ="http://www.some.dir/otherfeaturenamespace"
targetNamespace="http://www.some.dir/featurenamespace"
version="1.1.0"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:include schemaLocation="fe_types.xsd" />
<xs:import schemaLocation="ofe_types.xsd" namespace="http://www.some.dir/otherfeaturenamespace"/>
<xs:complexType name="complexElement">
<xs:sequence>
<xs:element name="feature" type="fe:FeatureType "/>
<xs:element name="otherfeature" type="ofe:otherFeatureType"/>
</sequence>
</xs:complexType>
The files: cmplxtypes.xsd, fe_types.xsd and ofe_types.xsd share the same namespace.
Now, trying to validate myxml against request.xsd I get: cannot resolve 'complexElement' to a 'type defintion' component.
I do appreciate any help.
There are several inconsistencies in the schemas that may explain the error.
request.xsd doesn't appear to be importing cmplxtypes.xsd, where fe:complexElement is defined.
<xs:import
schemaLocation="http://www.some.dir/featurenamespace/cmplxtypes.xsd"
namespace=" http://www.some.dir/featurenamespace" />
cmplxtypes.xsd includes ofe_types.xsd even though the namespace does not match according to the import in request.xsd. Namespaces much match when including other schemas (or the included schema must have no target namespace).
I want to create an XML schema that contains
<root>
<login>
<username> </username>
<paswword> </paswword>
</login>
</root>
How can i read this values and use them for authentication, with or without JAXB.
Here is the XML schema of your document:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="paswword" type="xs:string"/>
<xs:element name="username" type="xs:string"/>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="login"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="login">
<xs:complexType>
<xs:sequence>
<xs:element ref="username"/>
<xs:element ref="paswword"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Give that schema to some king of script that you are using to generate JAXB objects, pack them into a jar, put it into your classpath and use for your any purposes.
When using simple-xml, is there a way to let it ignore the nodes it does not recognize?
Yes. If you annotate your class with #Root(strict=false) it will ignore any elements that are not mapped. See the documentation for additional details:
http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#loosemap
On a related note, you can also handle optional elements using #Element(required=false).
http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#optional
Discliamer: If simple-xml means anything but a simple XML then the following answer is irrelevant
First, look at: http://www.w3.org/TR/xmlschema-1/#element-any
An example schema allowing such any elements is:
<?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:all>
<xs:element name="Element">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:any processContents="lax" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
And an example xml validating to the above is:
<?xml version="1.0" encoding="UTF-8"?>
<Root xsi:noNamespaceSchemaLocation="Any.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Element>
<Root>
<Element><Node1><SubElement/></Node1><Node2/></Element>
</Root>
</Element>
</Root>