Make xs and xmls:xsi attributes required in XSD - java

I have the following schema
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="translator">
...
</xs:element>
</xs:schema>
How I can define the following required attributes, so when adding a new translator node, those attributes are also added?
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Translator.xsd"
If I put them in XSD, like this
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="translator">
<xs:attribute name="xmlns:xsi" type="xs:string" default="http://www.w3.org/2001/XMLSchema-instance"/>
<xs:attribute name="xsi:noNamespaceSchemaLocation" type="xs:string" default="Translator.xsd"/>
</xs:element>
</xs:schema>
The following problem is reported by Xerces
[Error] :678:114: s4s-att-invalid-value: Invalid attribute value for 'name' in element 'attribute'. Recorded reason: cvc-datatype-valid.1.2.1: 'xmlns:xsi' is not a valid value for 'NCName'.
[Error] :678:114: src-attribute.3.1: One of 'ref' or 'name' must be present in a local attribute declaration.
[Error] :679:117: s4s-att-invalid-value: Invalid attribute value for 'name' in element 'attribute'. Recorded reason: cvc-datatype-valid.1.2.1: 'xsi:noNamespaceSchemaLocation' is not a valid value for 'NCName'.
[Error] :679:117: src-attribute.3.1: One of 'ref' or 'name' must be present in a local attribute declaration.

A warning first: the XML Schema specification forbids declaring attributes in the XML Schema instance namespace, and explicitly discourages attempts to alter its behavior.
Having said that, the reason for the errors you are getting is that the name attribute only supports definition of new elements in the target namespace (or in this case in no namespace) by providing their local names.
You could technically do something like this, by referencing the xsi:noNamespaceSchemaLocation attribute, which is already defined in the builtin XML Schema instance namespace:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xs:element name="translator">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xsi:noNamespaceSchemaLocation" default="Translator.xsd"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
However, you cannot change its definition, and since this attribute is builtin and processed in a special way, I am not sure you can much influence its behavior.

Related

Can XSD Assertions be Used in Specifying a WSDL File

I have a WSDL file which also contains all types used in it (via <wsdl:types>
tag). When defining the types, I have something like this:
<wsdl:definitions name="service"
targetNamespace="http://www.xxx.yyy/reg/definitions" xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tax="http://www.xxx.yyy/reg/definitions" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
.............
<xs:complexType name="CompanyName">
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="ShortName" type="xs:string" minOccurs="0" />
</xs:sequence>
<xs:attribute name="Language" type="tax:LanguageType" use="required"/>
<xs:assert test="ShortName or #Language != 'Language1'"/>
</xs:complexType>
.............
</wsdl:definitions>
Unfortunately, it doesn't work giving the following exception when I try to start the application on Tomcat:
javax.xml.ws.WebServiceException: org.xml.sax.SAXParseException; s4s-elt-invalid-content.1: The content of 'CompanyName' is invalid. Element 'assert' is invalid, misplaced, or occurs too often.
The WSDL version is 1.2 and I don't know which version of xsd it uses when types are described in it, so I don't know if it is a xsd version (1.0 vs. 1.1) issue or something else.
Can someone help me in finding the real issue?
EDIT: I have added the header of the wsdl.
I have added the version attribute (version="1.1") to <xs:schema>
definition but that didn't help either:
<xs:schema targetNamespace="http://www.xxx.yyy/reg/definitions" elementFormDefault="qualified" version="1.1">
Looks like assertions were introduced with XML Schema v1.1 when the XMLSchema definition moved over to w3.org.
Sample header:
<wsdl:description
targetNamespace="http://www.w3.org/2002/ws/sawsdl/spec/wsdl/order#"
xmlns="http://www.w3.org/2002/ws/sawsdl/spec/wsdl/order#"
xmlns:wsdl="http://www.w3.org/ns/wsdl"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sawsdl="http://www.w3.org/ns/sawsdl">

JAXB support for SOAP style arrayType

I'm trying to make a new version of a server that previously used Axis 1.4 to respond to SOAP RPC requests using Spring-WS. I have a few of the RPC calls working, but I'm stuck trying to satisfy a request that expects a SOAP body that looks like this:
<rpcCallResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<responseElement soapenc:arrayType="xsd:string[5]"
xsi:type="soapenc:Array"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<responseElement xsi:type="xsd:string">val1</responseElement>
<responseElement xsi:type="xsd:string">val2</responseElement>
<responseElement xsi:type="xsd:string">val3</responseElement>
<responseElement xsi:type="xsd:string" xsi:nil="true"/>
<responseElement xsi:type="xsd:string" xsi:nil="true"/>
</responseElement>
</rpcCallResponse>
I'm struggling to write the XML schema for this, and to get the JAXB marshaller to shove the xsi:type annotations into the response.
What's the correct XML schema to use/set of annotations to use to get this to marhsal (Java -> XML) correctly?
One solution I found that works for getting the arrayType added is to, instead of deriving from the http://schemas.xmlsoap.org/soap/encoding/ schema, use a custom schema of the form:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schemas.xmlsoap.org/soap/encoding/"
elementFormDefault="qualified">
<xs:attribute name="arrayType" type="xs:QName" />
</xs:schema>
...which replaces the type of the arrayType attribute with an xs:QName (vs. the actual type, which is just an xs:string). The advantage of using a QName seems to be that JAXB will take the QName's namespace and shove it onto the element when serialization happens -- which was the main blocker on getting a working schema above.
The schema for above now looks something like:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:encoding="http://schemas.xmlsoap.org/soap/encoding/"
targetNamespace="http://foo.com/bar"
elementFormDefault="qualified">
<xs:import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="soapenc.xsd" />
<xs:element name="rpcCallResponse">
<xs:complexType>
<xs:element name="responseElement">
<xs:complexType>
<xs:sequence>
<xs:element name="responseElement" maxOccurs="5" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:attribute ref="soapenc:arrayType" />
</xs:complexType>
</xs:element>
</xs:schema>

JAXB - Generate schema. Complex Type custom attribute

I need to generate following schema from java class using JAXB.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xdb="http://xmlns.oracle.com/xdb">
<xs:element name="test" type="test"/>
<xs:complexType name="testName" xdb:SQLType="WEBY_TEST_NAME">
<xs:sequence>
<xs:element minOccurs="0" name="date" type="xs:dateTime"/>
<xs:element name="id" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
How to add xdb:SQLType="WEBY_TEST_NAME" into complexType element using jaxb annotations ?
i done same try for generating the schema for different tags, but the names which include reserve words or else need to declare as
#XmlElement(name="class")
public String getClasss() {
return classs;
}
in pojo, so at time of coding it uses name which we provide, and in java program it uses the declared variables.
may be your declaration become
#XmlElement(name="xdb:SQLType")
public String getxdbSQLType() {
return xdbSQLType;
}

Set XmlType during xjc class generation

I'm using xjc via maven to generate sources. I'm using an XSD and a bindings file. I would like my generated classes to have the annotation #XmlType(name = ""). I can't see how to set the name to be blank.
I've tried (amongst other ideas) annotating using annox:annotate("http://annox.dev.java.net") with annox:class="javax.xml.bind.annotation.XmlType" but this adds another #XmlType annotation rather than replacing/overwriting the existing one.
Is there a way to set the #XmlType's name to be blank?
The name is left blank if the Type is an anonymous type. Check here (Section "Mapping a Class").
To do that, you need to declare your type inside an <element> tag. The following schema shows an example:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Container">
<xs:complexType>
<xs:sequence>
<xs:element ref="Item" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Item">
<xs:complexType >
</xs:complexType>
</xs:element>
</xs:schema>
Here, element Item is of an anonymous type, and here's the generated class:
#XmlType(name = "")
#XmlRootElement(name = "Item")
public class Item {
}

Java/XSD parsing

I doubt if there is something like this but I thought to ask though:
Does anyone know if there is a library in Java that reads an xsd file and "creates" the defined elements e.g. in a String format to use in the code?
E.g. read in the following schema:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="Street" type="xs:string" />
<xs:element name="Town" type="xs:string" />
<xs:element name="Country" type="xs:string" minOccurs="0" />
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And have a String in the following format:
<Address>
<Street></Street>
<Town></Town>
<Country></Country>
</Address>
Automatic tools do something similar, i.e. parse a WSDL and from the types section create for example JAXB classes that can be instances of the elements defined in schema.
Is there any library to do this?
UPDATE:
For example in Eclipse when creating an xml descriptor for a web application it presents a tree table with all the required elements for the users to fill in according to schema. How do they do it? I imagine they parse the xsds included in the jars
Any input is very welcome.
Thank you!
If its a WSDL file with which you want to generate Java classes, then Axis WSDL2Java (based on JAXB) can be used to get classes based on the schema defined in the WSDL.
JAXB also offers binding framework which you might want to look up.
http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.express.doc/info/exp/ae/twbs_jaxbschema2java.html
Above link should be useful.
oXygen has an XML instance generator that can generate a set of XML document samples based on a given XML Schema.
You can also invoke it from the commandline.

Categories

Resources