element with default enumeration value - java

Is it possible to set a default value for an element with type of enumeration?
I tried the following:
<xsd:element name="progressType" type="tns:progressType" default="Type A" nillable="false"/>
where tns:progressType is an enumeration:
<xsd:simpleType name="progressType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Type A" />
<xsd:enumeration value="Type B" />
</xsd:restriction>
</xsd:simpleType>
However, when I'm instantiating any object which contains the progressType attribute object.getProgressType() is always null.

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

wsimport not generating type class

I have the following element in my web service request and used wsimport to generate client. I also have other enum types which get classes generated. As this particular type contains values like 1-Critical I think it fails to generate enum type but treats it as String.
<xsd:element type="s0:UrgencyType" name="Urgency" nillable="true" minOccurs="0" maxOccurs="1"/>
<xsd:simpleType name="UrgencyType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="1-Critical"/>
<xsd:enumeration value="2-High"/>
<xsd:enumeration value="3-Medium"/>
<xsd:enumeration value="4-Low"/>
</xsd:restriction>
</xsd:simpleType>
The generated parameter as below.
#WebParam(name = "Urgency", targetNamespace = "urn:GIL:Create_WS")
String urgency,
I want the variable urgency type to be UrgencyType.

Element#getAttribute not returning default value when attribute isn't present

I have the following XML file:
<el id="el1">content</el>
The part of my file which defines the element above is as follows:
<xsd:element name="el" type="myns:el" />
<xsd:simpleType name="state">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="enabled" />
<xsd:enumeration value="disabled" />
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="el">
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="state" type="myns:state"
default="enabled" />
</xsd:complexType>
My Java code which is supposed to obtain the default value for state looks like the following:
Element e = (Element) ns.getElementsByTagName("el").item(0);
System.out.println(e.getAttribute("state"));
The code above always prints an empty string if I don't set the state attribute in the XML file. Why is that the case? According to the Java documentation, getAttribute returns
The Attr value as a string, or the empty string if that attribute does
not have a specified or default value.
My question is: why doesn't getAttribute return the default value when a value is not specified?

Swap XML elements using javax.xml.transform.Transformer

I have following xml portion
<xsd:complexType name="xyz">
<xsd:annotation>
<xsd:appinfo>
<Base ...../>
</xsd:appinfo>
</xsd:annotation>
<xsd:simpleContent>
<xsd:restriction base="niem-xsd:token">
<xsd:enumeration value="Consumer">
<xsd:annotation/>
</xsd:enumeration>
<xsd:enumeration value="abc">
<xsd:annotation/>
</xsd:enumeration>
<xsd:attributeGroup ref="s:SimpleObjectAttributeGroup"/>
I have to swap the enumeration elements and attributeGroup elements as floows
<xsd:complexType name="xyz">
<xsd:annotation>
<xsd:appinfo>
<Base ...../>
</xsd:appinfo>
</xsd:annotation>
<xsd:simpleContent>
<xsd:restriction base="niem-xsd:token">
<xsd:attributeGroup ref="s:SimpleObjectAttributeGroup"/>
<xsd:enumeration value="Consumer">
<xsd:annotation/>
</xsd:enumeration>
<xsd:enumeration value="abc">
<xsd:annotation/>
</xsd:enumeration>
. Can I do this using DTD file with javax.xml.transform.Transformer. How can I do this ?

javax.xml.transform.Transformer swaps my elements

I have following XSD section
<xsd:complexType name="xyz">
<xsd:annotation>
<xsd:appinfo>
<Base ...../>
</xsd:appinfo>
</xsd:annotation>
<xsd:simpleContent>
<xsd:restriction base="niem-xsd:token">
<xsd:enumeration value="Consumer">
<xsd:annotation/>
</xsd:enumeration>
<xsd:enumeration value="abc">
<xsd:annotation/>
</xsd:enumeration>
<xsd:attributeGroup ref="s:SimpleObjectAttributeGroup"/>
</xsd:restriction>
I use org.apache.ws.commons.schema class to build it and after some time I retrieve it using javax.xml.transform.Transformer class. When I transform it following is the result
<xsd:complexType name="xyz">
<xsd:annotation>
<xsd:appinfo>
<Base ...../>
</xsd:appinfo>
</xsd:annotation>
<xsd:simpleContent>
<xsd:restriction base="niem-xsd:token">
<xsd:attributeGroup ref="s:SimpleObjectAttributeGroup"/>
<xsd:enumeration value="Consumer">
<xsd:annotation/>
</xsd:enumeration>
<xsd:enumeration value="abc">
<xsd:annotation/>
</xsd:enumeration>
</xsd:restriction>
</xsd:simpleContent>
The enumeration and attributeGroup elements are changed the position and that causes validation error. Can I avoid this interchange in transformation ? What is going on there ?

Categories