i have simple xsd file:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
and some code to parse it
String aFile = "C:\\1.xsd";
XSDParser parser=null;
XSDSchema schema;
URI schemaUri = new File(aFile).toURI();
String uri;
try {
uri = schemaUri.toURL().toString();
parser=XSDParser.class.newInstance();
parser.parse(uri);
schema = parser.getSchema();
for (XSDElementDeclaration element : schema.getElementDeclarations()) {
System.out.println(element.getName());
}
Program prints "note". I debuged it, and can not find element to,from,heading and body. what do I change to receive output:
note
to
from
heading
body
org.eclipse.xsd.util.XSDPrototypicalSchema
Builds instances is likely to help you understand how to traverse them.
Related
i'm new to xsd and i want to generate xml like below with STUDENTRECORD occurring multiple times. i'm using jaxb to generate classes on the xsd
<STUDENTDETAIL>
<STUDENTINFORMATION>
<STUDENTRECORD>
<NAME>ABC</NAME>
<CLASS>4</CLASS>
<MAJOR>SCIENCE</MAJOR>
<GRADE>A</GRADE>
</STUDENTRECORD>
<STUDENTRECORD>
<NAME>DEF</NAME>
<CLASS>4</CLASS>
<MAJOR>SCIENCE</MAJOR>
<GRADE>B</GRADE>
</STUDENTRECORD>
</STUDENTINFORMATION>
My current xsd which generates STUDENTRECORD only once.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://webservice.com/WS" targetNamespace="http://webservice.com/WS" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Student" type="Student"/>
<xs:complexType name="Student">
<xs:sequence>
<xs:element name="STUDENTDETAIL">
<xs:complexType>
<xs:sequence>
<xs:element name="STUDENTINFORMATION">
<xs:complexType>
<xs:sequence>
<xs:element name="STUDENTRECORD">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="NAME"/>
<xs:element type="xs:string" name="CLASS"/>
<xs:element type="xs:string" name="MAJOR"/>
<xs:element type="xs:string" name="GRADE"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
Please help to fix.
Thanks
You just need to set a maxOccurs attribute on the STUDENTRECORD element declaration, like this:
<xs:element name="STUDENTRECORD" maxOccurs="unbounded">
This will allow the <STUDENTRECORD> to appear as many times you want. By default a given element is required to appear once.
Similarly, you can set minOccurs attribute to specify the minimal number of occurrences of an element.
I have defined following schema for XML
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.learnjava.com"
xmlns="http://www.learnjava.com"
elementFormDefault="qualified"
attributeFormDefault="qualified">
<!-- simple elements -->
<xs:element name="name" type="xs:string"/>
<xs:element name="hod" type="xs:string"/>
<xs:element name="dept" type="xs:integer"/>
<!-- attributes -->
<xs:attribute name="id" type="xs:integer"/>
<!-- complex elements -->
<xs:element name="department">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="hod" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="departments">
<xs:complexType>
<xs:sequence>
<xs:element ref="department" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="dept"/>
</xs:sequence>
<xs:attribute ref="id" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element ref="student" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="school">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="departments"/>
<xs:element ref="students"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
As per the schema, the attribute id is required for <department> and <student> tags and I have below XML which adheres to this rule
<?xml version="1.0" encoding="UTF-8"?>
<school xmlns="http://www.learnjava.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.learnjava.com ex2.xsd">
<name>TAMUC</name>
<departments>
<department id="1001">
<name>Computer Science</name>
</department>
<department id="1002">
<name>Social Science</name>
<hod>Jeff</hod>
</department>
</departments>
<students>
<student id="5001">
<name>Frank</name>
<dept>1001</dept>
</student>
<student id="5002">
<name>Paul</name>
<dept>1001</dept>
</student>
</students>
</school>
But the validation fails with below error
Error - Line 7, 25: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 25; cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'department'.
Error - Line 7, 25: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 25; cvc-complex-type.4: Attribute 'id' must appear on element 'department'.
[..further errors omited...]
Not sure for what is wrong. Both the error messages are contradictory
The validation fails if I remove "id" attributes from and as well as when I have it
I have a work around to make this work, by modifying the XSD like the one below
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.learnjava.com"
xmlns="http://www.learnjava.com"
elementFormDefault="qualified">
<xs:element name="school">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="departments">
<xs:complexType>
<xs:sequence>
<xs:element name="department" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="hod" type="xs:string" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element name="student" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="dept" type="xs:integer"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
but as an XML novice, I am curious to know what is wrong in the earlier XSD
PS:
1. I'm using http://www.utilities-online.info/xsdvalidation/#.VYpP8vmqqko to validate XML against the XSD.
2. I observe the same error in Altova XML Spy editor as well.
the problem is that the attribute id has been declared global(direct child of xs:schema). therefore, the attribute id should be qualified with the namespaces http://www.learnjava.com. Notice that id is not the same thing as http://www.learnjava.com:id
If you don't want that the attribute id belong to the namespace:
you should use a local attribute declaration
In your schema document, change
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.learnjava.com"
xmlns="http://www.learnjava.com"
elementFormDefault="qualified"
attributeFormDefault="qualified">
to
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.learnjava.com"
xmlns="http://www.learnjava.com"
elementFormDefault="qualified"
>
Update:
If you want to use global attribute declaration, then your xml instance document would look like the following:
<p:school xmlns:p="http://www.learnjava.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.learnjava.com ex2.xsd">
<p:name>TAMUC</p:name>
<p:departments>
<p:department p:id="1001">
<p:name>Computer Science</p:name>
</p:department>
<p:department p:id="1002">
<p:name>Social Science</p:name>
<p:hod>Jeff</p:hod>
</p:department>
</p:departments>
<p:students>
<p:student p:id="5001">
<p:name>Frank</p:name>
<p:dept>1001</p:dept>
</p:student>
<p:student p:id="5002">
<p:name>Paul</p:name>
<p:dept>1001</p:dept>
</p:student>
</p:students>
</p:school>
I'm trying to validate a simple XML using a simple XSD, but always get this error:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'linux'. One of '{linux}' is expected.
Why? The tag 'linux' is found and is one of {linux}!
java code:
public static void main(String[] args) {
try {
InputStream xml = new FileInputStream("data/test.xml");
InputStream xsd = new FileInputStream("data/test.xsd");
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource(xsd));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(xml));
log.info("OK!");
} catch (Exception e) {
log.error(":(");
log.error(e.getMessage());
}
}
data/test.xml:
<?xml version="1.0" encoding="utf-8"?>
<so xmlns="http://test/">
<linux>
<debian>true</debian>
<fedora>true</fedora>
</linux>
</so>
data/test.xsd
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://test/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="so">
<xs:complexType>
<xs:sequence>
<xs:element name="linux">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:any processContents="lax" maxOccurs="unbounded"/>
</xs:sequence></xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Because the schema does not specify elementFormDefault="qualified", the local element declaration of element "linux" is declaring an element in no namespace, but the instance has a linux element in namespace "http://test/". The error message is confusing because it fails to make clear that the problem is with the namespace.
Dudytz, your <xs:any /> is not correct. The document cannot be validated, because the validator needs to be instructed how to validate the document. If you don't want that, you can specifiy for the <xs:any> a processContents attribute. If you set this to "lax" or "skip", it will work.
In short: replace <xs:any> with <xs:any processContents="lax" />
Update: We have changed your XSD to a working version:
<xs:schema xmlns="http://test/" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test/">
<xs:element name="so">
<xs:complexType>
<xs:sequence>
<xs:element ref="linux"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="linux">
<xs:complexType>
<xs:sequence>
<xs:any processContents="lax" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
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.
I have the following XML file:
<?xml version="1.0"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library
xmlns="http://example.com/a"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com library.xsd"
name=".NET Developer's Library">
<book>
<category>computerss</category>
<title>Programming Microsoft .NET</title>
<author>Jeff Prosise</author>
<isbn>0-7356-1376-1</isbn>
</book>
<book>
<category>computer</category>
<title>Microsoft .NET for Programmers</title>
<author>Fergal Grimes</author>
<isbn>1-930110-19-7</isbn>
</book>
</library>
And the following Java code:
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
docBuilderFactory.setSchema(sf.newSchema(new File("library.xsd")));
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.parse(new FileInputStream("data.xml"));
It produces the following error:
[Error] :7:33: cvc-elt.1: Cannot find the declaration of element 'library'.
If I remove the XSD declaration in the XML file everything works fine...
Any inside highly appreciated. Thanks.
And here is the schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="category"/>
<xs:element ref="title"/>
<xs:element ref="author"/>
<xs:element ref="isbn"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="category">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="computer" />
<xs:enumeration value="poetry" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="isbn" type="xs:string"/>
</xs:schema>
Your XML has a reference to namespace (xmlns="http://example.com/a") which is not the same as in your schema. Have you tried to validate your XML against schema in any XML editor (e.g. Altova or Eclipse etc).
So far it looks like your parsing error is legit, XML is not valid according to the schema.
Your schema definition is incorrect. For a start, as maximdim says, your schema has no targetNamespace="http://mysite.com/a" attribute in the schema tag.
Secondly your schema looks as if it should only have a single root element, yours has 6.
A correct schema for your XML instance would be:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://mysite.com/a" targetNamespace="http://mysite.com/a">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="book" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="book">
<xs:sequence>
<xs:element name="category">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="computer" />
<xs:enumeration value="poetry" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="isbn" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>