XSD Basic Help Needed -- Namespaces in Schemas? - java

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.

Related

Java XML Create Fragment/Elements Without Namespace

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>

xml schema that generated is showing error

I want to create a Java bean using the XML schema. I tried to create an XML schema from XML using freeformatter.com's XSD/XML Schema Generator. This is the XML schema that I have generated and is producing an error:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://open-services.net/ns/core#" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shortTitle">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="rdf:parseType" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="discussedBy">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="rdf:resource" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="instanceShape">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="rdf:resource" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="serviceProvider">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="rdf:resource" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
This is the error:
- src-resolve.4.2: Error resolving component 'rdf:parseType'. It was detected that 'rdf:parseType' is in namespace 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', but
components from this namespace are not referenceable from schema document 'file:///home/workspace/jaxbexe/xsdfile.xsd'. If this is the incorrect namespace,
perhaps the prefix of 'rdf:parseType' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///home/
workspace/jaxbexe/xsdfile.xsd'.
- s4s-elt-invalid-content.1: The content of '#AnonType_shortTitle' is invalid. Element 'attribute' is invalid, misplaced, or occurs too often.
Yes, this free tool seems to be unable to cope with namespaces. So either complain to the author to get it fixed, or use a different tool - there are plenty around.

Error while generating JAXB model from xsd in eclipse

I am trying to generate JAXB model from the following xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://abc.com/mmm/trt" attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:annotation>
<xs:documentation>Schema for Cache Refresh Event.
</xs:documentation>
</xs:annotation>
<xs:simpleType name="cacheCode">
<xs:restriction base="xs:string">
<xs:enumeration value="BUSINESS_RULE" />
<xs:enumeration value="USER" />
</xs:restriction>
</xs:simpleType>
<xs:element name="cacheRefreshEvent">
<xs:complexType>
<xs:sequence>
<xs:element name="cacheCode" type="cacheCode" minOccurs="1"
maxOccurs="1" />
<xs:element name="entityRefId" type="xs:string" minOccurs="0"
maxOccurs="20" />
<xs:element name="cacheRefreshDate" type="xs:dateTime"
minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But, it is giving me the following error, which i got in Eclipse console
parsing a schema...
[ERROR] src-resolve.4.1: Error resolving component 'cacheCode'. It was detected that 'cacheCode' has no namespace, but components with no target namespace are not referenceable from schema document 'file:/F:/Sprint6/cache/src/main/resources/CacheRefreshEvent.xsd'. If 'cacheCode' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'cacheCode' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:/F:/Sprint6/cache/src/main/resources/CacheRefreshEvent.xsd'.
line 18 of file:/F:/Sprint6/cache/src/main/resources/CacheRefreshEvent.xsd
Failed to parse a schema.
Please help.
Remove this extra namespace attribute targetNamespace="http://www.hcentive.com/mea/trr"
OR add this xmlns="http://www.hcentive.com/mea/trr"
Both solutions will work :)
For you reference targetnamespace-and-xmlns

Why does JAXB not create a member variable in its generated code when an XML schema base type and subtype have the same element declared in them?

I have a question regarding with regard to JAXB generated classes.
As you can I see, I have a complex type, DG_PaymentIdentification1, declared in my schema.
Its a restriction of PaymentIdentification1. DG_PaymentIdentification1 is also identical to
PaymentIdentification1. I also have a type called DG_CreditTransferTransactionInformation10
which has a base type of CreditTransferTransactionInformation10 and is identical to it.
I have included the relevant XML schema snippets below.
<xs:complexType name="DG_PaymentIdentification1">
<xs:complexContent>
<xs:restriction base="PaymentIdentification1">
<xs:sequence>
<xs:element name="InstrId" type="DG_Max35Text_REF" minOccurs="0"/>
<xs:element name="EndToEndId" type="DG_Max35Text_REF" id="DG-41"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="PaymentIdentification1">
<xs:sequence>
<xs:element name="InstrId" type="Max35Text" minOccurs="0"/>
<xs:element name="EndToEndId" type="Max35Text"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="DG_CreditTransferTransactionInformation10">
<xs:complexContent>
<xs:restriction base="CreditTransferTransactionInformation10">
<xs:sequence>
<xs:element name="PmtId" type="DG_PaymentIdentification1"/>
<xs:simpleType name="DG_Max35Text_REF">
<xs:restriction base="DG_NotEmpty35">
<xs:pattern value="[\-A-Za-z0-9\+/\?:\(\)\.,' ]*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="Max35Text">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="35"/>
</xs:restriction>
</xs:simpleType>
JAXB generates the following java class for DG_PaymentIdentification1:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "DG_CreditTransferTransactionInformation10")
public class DGCreditTransferTransactionInformation10
extends CreditTransferTransactionInformation10
{
}
My question is why doesnt the DGCreditTransferTransactionInformation10 generated class have a variable
of type DG_PaymentIdentification1 in the generated code? The base class CreditTransferTransactionInformation10
does have a type PaymentIdentification1 declared in it.
Is there any way of ensuring that DGCreditTransferTransactionInformation10 will have a DG_PaymentIdentification1
in it?

Parse types in XML schema. Java

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.

Categories