Namespace error in XSD - java

The error when validated using SAX parser is:
"org.xml.sax.SAXParseException; systemId: file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd; lineNumber: 10; columnNumber: 31; src-resolve.4.1: Error resolving component 'name'.
It was detected that 'name' has no namespace, but components with no target namespace are not referenceable from schema document 'file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd'.
If 'name' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'name' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd'."
address.xml
<?xml version ="1.0" encoding="UTF-8"?>
<address
xmlns:personal="Personal things"
xmlns:houses="Regarding to houses"
xmlns="http://www.w3schools.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
xsd:schemaLocation="address.xsd"
>
<name>
<personal:title>Mr.</personal:title>
<first-name>Samitha</first-name>
<last-name>Chathuranga</last-name>
</name>
<sssd></sssd>
<house-id>
<houses:title>107 B</houses:title>
<NAME>Sam&apos;s Home</NAME>
<!-- An intnal entity is used for the single quote in House Name here-->
</house-id>
<village>Poramba</village>
<city district="Galle" province="Southern">AG</city>
<postal-code>80300</postal-code>
<country>Sri Lanka</country>
</address>
address.xsd
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/SampleSchema"
xmlns:tns="http://www.example.org/SampleSchema"
>
<xsd:element name="address">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="name" />
<xsd:element ref="house-id" />
<xsd:element ref="village" />
<xsd:element ref="city" />
<xsd:element ref="postal-code" />
<xsd:element ref="country" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="name">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="title" />
<xsd:element ref="first-name" />
<xsd:element ref="last-name" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="title" type="xsd:string" />
<xsd:element name="first-name" type="xsd:string" />
<xsd:element name="last-name" type="xsd:string" />
<xsd:element name="house-id">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="title" />
<xsd:element ref="NAME" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="NAME" type="xsd:string" />
<xsd:element name="village" type="xsd:string" />
<xsd:element name="country" type="xsd:string" />
<xsd:element name="city">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:length value="2" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="postal-code">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{5}(-[0-9]{4})?" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:schema>
How to resolve this error?

Well your xsd is invalid. You are constructing it wrong. Here are my suggestions. Download XMLSpy or Liquid XML studio and then view my examples to see how to construct a proper XSD and then validate the XSD against it. These editors are a great way to visually see the XML documents and xsd's. They will help a lot.
Essentially you need to declare your types in the XSD then create elements based on those types. While your approach can work I would suggest that you study the approach I have taken it is pretty easy. I could explain it all but I think you will get the idea rather quickly.
Address.xsd
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2013 Designer Edition 11.1.0.4725 (http://www.liquid-technologies.com)-->
<xsd:schema xmlns:address="http://www.example.org/AddressSchema"
targetNamespace="http://www.example.org/AddressSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="address">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="person_name"
type="address:person_name_type" />
<xsd:element name="ssd"
type="address:ssd_type" />
<xsd:element name="house_id"
type="address:house_id_type" />
<xsd:element name="village"
type="address:village_type" />
<xsd:element name="city"
type="address:city_type" />
<xsd:element name="postal_code"
type="address:postalcode_type" />
<xsd:element name="country"
type="address:country_type" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="name_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="postalcode_type">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{5}(-[0-9]{4})?" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="city_type">
<xsd:restriction base="xsd:string">
<xsd:length value="2" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="village_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="country_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="title_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="first_name_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="last_name_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:complexType name="house_id_type">
<xsd:sequence>
<xsd:element name="title"
type="address:title_type" />
<xsd:element name="name"
type="address:name_type" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="person_name_type">
<xsd:sequence>
<xsd:element name="title"
type="address:title_type" />
<xsd:element name="first_name"
type="address:first_name_type" />
<xsd:element name="last_name"
type="address:last_name_type" />
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="ssd_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:schema>
Address.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML 2013 Designer Edition 11.1.0.4725 (http://www.liquid-technologies.com) -->
<address xsi:schemaLocation="http://www.example.org/AddressSchema D:\GroundZero\address.xsd" xmlns="http://www.example.org/AddressSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<person_name>
<title>string</title>
<first_name>string</first_name>
<last_name>string</last_name>
</person_name>
<ssd>string</ssd>
<house_id>
<title>string</title>
<name>string</name>
</house_id>
<village>string</village>
<city>AB</city>
<postal_code>80300</postal_code>
<country>string</country>
</address>

I would also note that your instance document has the root (address) element in namespace http://www.w3schools.com, while your schema defines an address element in namespace http://www.example.org/AddressSchema. You can't just sprinkle namespaces around like fairy-dust and hope they add glamour to your code; they are fundamental and you need to get them right.

Related

Compare XML using xmlunit when order of Attributes is different is not working

I have 2 xmls : Basically two XSD schemas
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="my_export_file">
<xsd:complexType>
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="row">
<xsd:complexType>
<xsd:attributeGroup ref="rowattr" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attributeGroup ref="docelattr" />
</xsd:complexType>
</xsd:element>
<xsd:attributeGroup name="rowattr">
<xsd:attribute name="subject_level_ind" type="Str.1" use="optional" />
**<xsd:attribute name="object_level_ind" type="Str.1" use="optional" />**
<xsd:attribute name="src_system_id" type="Str.80" use="required" />
</xsd:attributeGroup>
<xsd:attributeGroup name="docelattr">
<xsd:attribute name="reporting_date" type="xsd:string" />
<xsd:attribute name="interface_type" type="xsd:string" />
</xsd:attributeGroup>
<xsd:simpleType name="Str.1">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="1" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="Str.80">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="80" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
and
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="my_export_file">
<xsd:complexType>
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="row">
<xsd:complexType>
<xsd:attributeGroup ref="rowattr" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attributeGroup ref="docelattr" />
</xsd:complexType>
</xsd:element>
<xsd:attributeGroup name="rowattr">
<xsd:attribute name="subject_level_ind" type="Str.1" use="optional" />
<xsd:attribute name="src_system_id" type="Str.80" use="required" />
**<xsd:attribute name="object_level_ind" type="Str.1" use="optional" />**
</xsd:attributeGroup>
<xsd:attributeGroup name="docelattr">
<xsd:attribute name="reporting_date" type="xsd:string" />
<xsd:attribute name="interface_type" type="xsd:string" />
</xsd:attributeGroup>
<xsd:simpleType name="Str.1">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="1" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="Str.80">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="80" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
I want to compare those and give differences. My code works fine but the only issue is if order of attributes is different it doesn't treat them as "SIMILAR". As you can see in the example, my xmls are same with just one change - order of object_level_ind is different. I want my code to not return this difference.
Code
var fis1 = new FileReader("C:\\Users\\test1.xsd ");
var fis2 = new FileReader("C:\\Users\\test2.xsd");
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreAttributeOrder(true);
DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(fis1,fis2));
diff.overrideElementQualifier(new ElementNameAndTextQualifier());
List<?> allDifferences = diff.getAllDifferences();
System.out.println(allDifferences);
I Also tried:
DifferenceEvaluator evaluator = DifferenceEvaluators
.downgradeDifferencesToEqual(ComparisonType.CHILD_NODELIST_SEQUENCE);
Diff diff = DiffBuilder.compare(fis1)
.withTest(fis2).ignoreComments()
.ignoreWhitespace()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
.withDifferenceEvaluator(evaluator)
.checkForSimilar()
.build();
System.out.println("Differences: " + diff);
I Also tried solution given in comparing two xmls using xmlunit ignorng their order
But for my xml it gives:
identical: false
similar : false
Please let me know if any pointers.
Best Regards,
Abhi
This is happening because setIgnoteAttributeOrder ignores the order of the attributes of a node/element and not the actual order of node/element in the XML document. So, while <xsd:attribute name="object_level_ind" type="Str.1" use="optional" /> and <xsd:attribute use="optional" name="object_level_ind" type="Str.1" /> are considered same, the order of the elements is not ignored.
This answer here may have more details on how to compare ignoring element order - Compare two XML strings ignoring element order

Error while trying to create Java Class out of .xsd file

I have two XMLs and i want convert them into java classes so i went to this page where i can generate .xsd files. I'm trying to make two Java Class out of them in Eclipse and I'm getting the same error:
parsing a schema...
[ERROR] Content is not allowed in prolog.
line 1 of file:/(...)/queryClass.xsd
Failed to parse a schema.
These are my .xsd files:
First:
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="dao" type="daoType" />
<xsd:complexType name="daoType">
<xsd:sequence>
<xsd:element name="query" type="queryType" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="queryType">
<xsd:sequence>
<xsd:element name="arguments" type="argumentsType" />
<xsd:element name="statement" type="xsd:string" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="argumentsType">
<xsd:sequence>
<xsd:element name="argument" type="argumentType" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="argumentType">
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:schema>
Second:
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="properties" type="propertiesType" />
<xsd:complexType name="propertiesType">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="entry" type="entryType" />
</xsd:sequence>
<xsd:attribute name="version" type="xsd:decimal" />
</xsd:complexType>
<xsd:complexType name="entryType">
<xsd:attribute name="key" type="xsd:string" />
</xsd:complexType>
</xsd:schema>
It turned out that the XSD files were wrong. I used another XML-XSD converter and it worked perfectly fine.

How to create all possible XMLs?

How can I create all possible XMLs of these XSD:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns="http://beep2000/client/beep.de" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://beep2000/client/beep.de">
<xsd:complexType name="TextType">
<xsd:sequence>
<xsd:element name="Text" minOccurs=1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="255"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="StreetType">
<xsd:sequence>
<xsd:element name="Street" type="xsd:string" minOccurs="1"/>
<xsd:element name="HouseNumber" type="xsd:int" minOccurs="1"/>
<xsd:element name="Suffix" type="xsd:string" minOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="1"/>
<xsd:enumeration value="A"/>
<xsd:enumeration value="B"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BodyType">
<xsd:sequence>
<xsd:element name="FirstPart" type="TextType" minOccurs="1"/>
<xsd:element name="SecondPart" type="TextType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="AdressType">
<xsd:sequence>
<xsd:element name="Street" type="StreetType" minOccurs="0">
<xsd:element name="PostalCode" minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="5"/>
<xsd:pattern value="[0-9]{5}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="City" type="xsd:string" minOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="RequestEnvelope">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" minOccurs="0"/>
<xsd:element name="Forename" type="xsd:string" minOccurs="1"/>
<xsd:element name="Surname" type="xsd:string" minOccurs="0"/>
<xsd:element name="PersonalID" minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Adress" type="AdressType" minOccurs="0"/>
<xsd:element name="Body" type="BodyType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ResponseEnvelope">
<xsd:sequence>
<xsd:element name="Result" minOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="3"/>
<xsd:enumeration value="OK"/>
<xsd:enumeration value="NOK"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Errorcode" type="xsd:integer" minOccurs="0"/>
<xsd:element name="Errortext" minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="255"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Comment" type="TextType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="MsgResponse" type="ResponseEnvelope"/>
<xsd:element name="MsgRequest" type="RequestEnvelope"/>
</xsd:schema>
There are some fields which are optional and some field which are mandatory. So there is a variety of XMLs. How can I find all possible structures of XMLs?
I expect something like:
XML No.1:
Field 1 --> mandatory
Field 2 --> mandatory
Field 3 --> optional
......
XML No.2.:
Field 1 --> mandatory
Field 2 --> mandatory
Field 4 --> mandatory
.....
Generating all possible XML files that conform to this schema would take an infinite amount of time and they would occupy an infinite amount of space.
However, you can generate a selection of sample documents using tools such as those described here:
How to generate sample XML documents from their DTD or XSD?

Java unable to generate add methods from WSDL

I have this type of array in WSDL file:
<xsd:element name="Entries">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="id" type="xsd:int"
minOccurs='0' maxOccurs='unbounded' />
<xsd:element name="brand" type="xsd:string"
minOccurs='0' maxOccurs='unbounded' />
<xsd:element name="model" type="xsd:string"
minOccurs='0' maxOccurs='unbounded' />
<xsd:element name="engine" type="xsd:string"
minOccurs='0' maxOccurs='unbounded' />
<xsd:element name="highway_length" type="xsd:int"
minOccurs='0' maxOccurs='unbounded' />
<xsd:element name="city_length" type="xsd:int"
minOccurs='0' maxOccurs='unbounded' />
<xsd:element name="fuel_consumed" type="xsd:int"
minOccurs='0' maxOccurs='unbounded' />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
When I generate server side classes, adders are generated only for brand, model and engine and not for others.
I have tried using xsd:integer type and then adders where generated, but in Java this type is BigInteger, which is not necessary.
My question is, how should I change my WSDL file in order to make sure that add methods are generated for all elements?
EDIT: This question is still not answered, I just used xsd:Integer types and in Java casted BigIntegers to ints.

Restrict value of an XML attribute to different attribute's/element's value

I will start with an example
<template>
<components>
<component name="switch" />
<component name="server" />
</components>
<layout>
<grid>
<position componentName="switch" positionX="0" positionY="0" />
</grid>
</layout>
</template>
What I want is to restrict values in componentName attribute to match one of the names specified above in components. Is this possible in JAXB? Because I need to have annotated classes which are then used to generate XSD.
Given your scenario, XSD 1.0 can enforce your "referential integrity" through a key/keyref combo. However, I am not aware of what annotations there are for these constructs in JAXB (it sounds as if you're looking at generating an XSD from you Java classes); at least I never ran into such annotations (see the list here)
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="template">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="components">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="component">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="layout">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="grid">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="position">
<xsd:complexType>
<xsd:attribute name="componentName" type="xsd:string" use="required" />
<xsd:attribute name="positionX" type="xsd:unsignedByte" use="required" />
<xsd:attribute name="positionY" type="xsd:unsignedByte" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="ComponentsKey">
<xsd:selector xpath="components/component"/>
<xsd:field xpath="#name"/>
</xsd:key>
<xsd:keyref name="MatchComponent" refer="ComponentsKey">
<xsd:selector xpath="layout/grid/position"/>
<xsd:field xpath="#componentName"/>
</xsd:keyref>
</xsd:element>
</xsd:schema>

Categories