cXML to java bindings issues - xsd

Issue: Unable to create java classes from cXML.dtd using java xjc
version I am using is 1.2.032
command used : xjc -dtd cXML.dtd
Error :
parsing a schema...
[ERROR] Property "Name" is already defined. Use <jaxb:property> to resolve th
is conflict.
Issue 1 : Line number around 573
issue is with the "name" as its duplicate (element as well as attribute).
issue 2:
ShippingPaymentMethod,TermsOfDeliveryCode,TransportTerms uses "value" which is causing duplicate definations.
Solution as I understand==
I need custom binding.xml .. I tried various ways but unable to create correct binding.xml to solve this issue. once I have correct xml I can use following command to create generated classes.
xjc -b binding.xml -dtd cXML.dtd
What I help I need
please provide correct binding.xml if possible
Is there any alternate way to generate java mappings for this cXML
Is there possibilities to have XSD and then have java mapping from XSD?
Please suggest.

FYI: you can also solve this with an external jax-b binding file that looks like this:
<xml-java-binding-schema xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<element name="ReturnData" type="class">
<attribute name="name" property="nameAttribute"/>
</element>
<element name="ShippingPaymentMethod" type="class">
<attribute name="value" property="valueAttribute"/>
</element>
<element name="TermsOfDeliveryCode" type="class">
<attribute name="value" property="valueAttribute"/>
</element>
<element name="TransportTerms" type="class">
<attribute name="value" property="valueAttribute"/>
</element>
</xml-java-binding-schema>
The CXML spec is VERY annoying to generate JAX-B classes for because of their continued use of DTD over XML schema. This is especially annoying if you want to use the other DTDs (Invoice, Catalog, Fulfill) as they each redefine all the common elements but use their own versions of the cxml.requests, cxml.messages, cxml.responses entities

Issue Resolved by myself. did following steps.. may be useful for others
Renamed "name" element "ReturnData" in Cxml.dtd
Renamed "value" attr from TransportTerms,ShippingPaymentMethod,and TermsOfDeliveryCode in Cxml.dtd
Created java classes using
xjc -dtd cXML.dtd
4.in Generated java classes changed xml annotation back to original.
So method names will be different but it will read and write correct XML.

Related

XSD having metadata at each field level

i have XML structure as below (just part of large XML)
<Person>
<firstName>
<lastName>
<Partner>
<firstName>
...
</Person>
i need to keep additional metadata with each field for example to indicate if its updatable or not
i c two approaches
1) add the metadata at each FIELD level. i feel this overly complicates the XSD as each element is now an OBJECT
<Person>
<firstName updatable="true" ... />
...
</Person>
2) separate out the metadata as below
BUt how do i link the metadata to the data? via a uniquie name? can someone consuming the XML easily link it?
Is there a better way? Thanks!
<data>
<Person>one
<firstName>
<lastName>
<Partner>
<firstName>
...
</Person>
<Person>two
<firstName>
<lastName>
<Partner>
<firstName>
...
</Person>
</data>
<metadata>
<field name="firstName" updateble="false"/>
....
</metadata>
i think this is similar to
Add metadata to an XSD definition
but it does not have any answer
My (maybe rhetoric) question would be why would someone want to see this metadata with each XML if it is static in relationship to the model?
I'll show you a UBL XSD snippet (XML namespaces elided as irrelevant):
<xsd:element ref="cbc:UBLVersionID" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>BBIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Application Response. UBL Version Identifier. Identifier</ccts:DictionaryEntryName>
<ccts:Definition>The earliest version of the UBL 2 schema for this document type that defines all of the elements that might be encountered in the current instance.</ccts:Definition>
<ccts:Cardinality>0..1</ccts:Cardinality>
<ccts:ObjectClass>Application Response</ccts:ObjectClass>
<ccts:PropertyTerm>UBL Version Identifier</ccts:PropertyTerm>
<ccts:RepresentationTerm>Identifier</ccts:RepresentationTerm>
<ccts:DataType>Identifier. Type</ccts:DataType>
<ccts:Examples>2.0.5</ccts:Examples>
</ccts:Component>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
You could see here that there's a lot of structured data which could easily pass as something related to your question. Fundamentally though, this is a mechanism that uses the XSD annotations mechanism, to achieve things in relationship to the XSD. Another one is that used by JAXB custom binding mechanism:
<xsd:simpleType name="ZipCodeType">
<xsd:annotation>
<xsd:appinfo>
<jxb:javaType name="int" parseMethod="javax.xml.bind.DatatypeConverter.parseInt" printMethod="javax.xml.bind.DatatypeConverter.printInt"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="10000"/>
<xsd:maxInclusive value="99999"/>
</xsd:restriction>
</xsd:simpleType>
While this is different than the UBL example, (documentation vs. appinfo), both are using the XSD annotation mechanism.
JAXB's custom bindings also supports a model where the custom bindings are separate from the XSD (in their own separate XML file). The correlation between the custom binding file (the metadata in your case) and the XSD (the XML in your case) is done through XPath matches.
This brings about another clarification: what is the processing model you have in mind? Dynamic (i.e. the metamodel is static, but can be applied to arbitrary XSDs)? Platform? Below is a solution that could work for what you need, in a dynamic fashion, if it happens to match your platform.
.NET:
Build an XSD the way I've referred to above (i.e. annotations of some sort).
At runtime, validate your XML against this XSD. Each node will then have the SchemaInfo property filled in. Using classes in System.Xml.Schema you could easily process the SchemaElement or SchemaAttribute in your SchemaInfo property as an XmlSchemaAnnotated class, which is what both are.
The above is basically PSVI applied to your XML. The same exists for Java (on this Xerces page search for How do I retrieve PSVI from the DOM?)...
I could picture solutions for XSLT as well, or not involving XSD at all... the above though should be sufficient to get you started.

Troubles converting XSD to Java using JAXB

I'm trying to convert an XSD I have no control over to Java classes using JAXB. The errors I'm getting are :
[ERROR] cvc-pattern-valid: Value 'true' is not facet-valid with respect to pattern '0|1' for type 'BooleanType'.
line 139 of http://neon/meaweb/schema/common/meta/MXMeta.xsd
[ERROR] a-props-correct.2: Invalid value constraint value '1' in attribute 'mxencrypted'.
line 139 of http://neon/meaweb/schema/common/meta/MXMeta.xsd
The code in the XSD that contains the error is in:
<xsd:complexType name="MXCryptoType">
<xsd:simpleContent>
<xsd:extension base="xsd:base64Binary">
<xsd:attribute name="changed" type="ChangeIndicatorType" use="optional" />
<xsd:attribute name="mxencrypted" type="BooleanType" use="optional" default="1" />
</xsd:extension>
</xsd:simpleContent>
Specifically it's the attribute mxencrypted using the BooleanType. BooleanType is defined as
<xsd:simpleType name="BooleanType">
<xsd:restriction base="xsd:boolean">
<xsd:pattern value="0|1" />
</xsd:restriction>
</xsd:simpleType>
From searching around this seems to be a somewhat common case. From what I can tell the default in the mxencrypted line shouldn't be a 1? When I load the XSD into Liquid XML, the schema doesn't report errors. Validating the XSD here (http://www.utilities-online.info/xsdvalidation/#.UV3zkL_EW0s) reports the same errors as JAXB.
Is there a way to tell JAXB to ignore this problem and just generate the class ignoring the default?
Your question is similar to this one (and I've just updated it with relevant information). I am not aware of a way to tell JAXB to ignore it, since this error happens in the XSD schema processor (before JAXB's xjc starts to do its work actually).
The only way may be to filter out the default attributes; however, in this case it is obvious that the XSD designer intended to have a default value of true, which would not be the case with your generated code (Java defaults to false).
This could yield unwanted fracas, my recommendation would be to work with the XSD provider to get it fixed.
Maybe a sidebar, but I personally consider the use of defaults in XSDs as an interoperability monster : any XML processor that is not relying on the XSD would behave differently than one that does.

How to create xsd for an object

I have an method in which I need to pass parameters value some like this in the publish methods.
facebookclient. publish("me/events", FacebookType.class,Parameter.with("name", "Party"));
Instead of passing it as three parameters, I am trying to pass it as object storing these three parameters in the object record.
eg:
facebookclient.publish(record);
so that in publish method , I can get this
void publish(Record record)
{
String event= record.getEvent();
}
I am trying to create an xsd for these parameters. Kindly guide me how to do it. for me/events I can create an element in the xsd as
<element name="events" type="string" />
and refer it in an object
<element name="Record">
<complexType>
<sequence>
<element ref="fb:events" />
// how to specify for FacebookType.class,Parameter.with("name", "Party"))?
</sequence>
</complexType>
</element>
Kindly guide me, how to specify the element FacebookType.class,Parameter.with("name", "Party")) in xsd?
You can use the schemagen application (http://pic.dhe.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=%2Fcom.ibm.websphere.wsfep.multiplatform.doc%2Finfo%2Fae%2Fae%2Ftwbs_jaxbjava2schema.html) to do it via command line or JAXB classes (http://jaxb.java.net/guide/Invoking_schemagen_programatically.html) programmatically... this is all assuming you have you classes annotated with the appropriate annotations.

using xsd attributes in another xsd as tag/tag-parameters

I wanted to know if it is possible or not:
a.xsd :
<attribute name="aa" type="String">
b.xsd
<element name="bb" aa="pan" type="string"/>
or
<aa name="pan" type="string">
basically i am trying to find out if we can use the xsd attributes inside another xsd as tags or tag-parameters.
i am new to this xsd world if it is wrong use-case then also please post your views.
I am not clear on what you are trying to accomplish, but the xsd:element element has a set of recognized attributes. It won't do you any good to try to make up attributes to add to it.

In an XSD schema, how do I say that an element might have any number of subelements that must inherit from a certain type?

Say I have these types defined in my XSD:
<complexType name="NamedEntity">
<attribute name="ix" type="positiveInteger"></attribute>
<attribute name="sName" type="string"></attribute>
<attribute name="txtDesc" type="string"></attribute>
</complexType>
<complexType name="Node">
<complexContent>
<extension base="tns:NamedEntity">
</extension>
</complexContent>
</complexType>
<complexType name="Source">
<complexContent>
<extension base="tns:NamedEntity">
<attribute name="dt" type="dateTime"></attribute>
</extension>
</complexContent>
</complexType>
Now I want to express that a Node element may have zero or more child elements that may be of the type Node or Source.
It would be OK if I had to somehow enumerate the allowed types for the children, but since I have more types that inherit from NamedEntity, it would be neat if I could specify just the base type.
Edit: I'd rather not use xsi:type in the document but have a unambigous relationship between element name and type. Quite a lot XML processing seems to depend on that, and I also find it a lot more readable.
Please don't use xsi:type if you can avoid it. It's evil. Ok, maybe I exaggerate, but it does make it impossible to parse the document without intimate knowledge of the schema, which is bad enough in practice.
What will help you is: substitutionGroup.
In the schema, have the Node element contain zero or more child elements of type NamedEntity. In the actual document, use the xsi:type attribute (xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance") to choose the subtype ("Node" or "Source") for each one.
This may be beyond the capabilities of XSD. Have you considered doing extra validation using Schematron?
I think you want a substitution group.

Resources