Handling a leading zero in a wsdl - xsd

I got provided an XSD that had a type representation as follows:
<xs:element name="PhoneNumber">
<xs:simpleType>
<xs:restriction base="xs:nonNegativeInteger">
<xs:pattern value="04[0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Now when we use Apache CXF to convert this from the XSD to autogenerated code which assigns it as a BigInteger.
So what I would like to know:
Is that piece of XSD valid? (I tend to think not as an Integer will never exist with a leading zero)
Is there anyway to get Apache CXF to handle such a condition and force the type to be a string?

Related

xmpp : XML schema

I am a beginner in XML schema and XMPP message protocols. Is there any explicit way to understand the below XML schema. What does each tags means and how many types of tags are there - what is the specified way of reading and writing them:
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema
xmlns:xs='http://www.w3.org/2001/XMLSchema'
targetNamespace='http://jabber.org/protocol/address'
xmlns='http://jabber.org/protocol/address'
elementFormDefault='qualified'>
<xs:annotation>
<xs:documentation>
The protocol documented by this schema is defined in
XEP-0033: http://www.xmpp.org/extensions/xep-0033.html
</xs:documentation>
</xs:annotation>
<xs:element name='address'>
<xs:complexType>
<xs:simpleContent>
<xs:extension base='empty'>
<xs:simpleType>
<xs:restriction base='xs:NCName'>
<xs:enumeration value='bcc'/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name='uri' use='optional' type='xs:string'/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:simpleType name='empty'>
<xs:restriction base='xs:string'>
<xs:enumeration value=''/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
XSD is a complex language, and there's no way of giving an adequate explanation in a few paragraphs. Reading the W3C XML Schema Primer (XML Schema Part 0) might be a good start; even better, get Eric van der Vlist's book and read the opening chapters.
The fragment you have given us is not well-formed: it contains an unmatched </xs:attribute> end tag at line 26. If we add the missing line:
<xs:attribute name='type' use='required'>
at line 21, then the schema is defining rules for an address element that has two attributes and no content: a mandatory type attribute whose only permitted value is bcc, and an optional uri attribute whose value is any string. It's a rather odd way to define an element with no content: it's actually defining it as an element that does have content, where the content must be an empty string. Perhaps the schema designers had a reason for this, but it's not immediately obvious.

JAXB Schema design, having enum dynamic value

I have following schema
<xs:simpleType name="enumType">
<xs:restriction base="xs:string">
<xs:enumeration value="STRING_ONE"/>
<xs:enumeration value="STRING_TWO"/>
</xs:restriction>
</xs:simpleType>
As long as i get a known string, I can swithc and it to my jaxbobject which expects a enumType object. But what if i don't know this string ? is there a way to handle it based on JAXB Schema ? I know based on enum its not possible.
Edit: Trying to make it more clear
Wit the given Schema design, my JAXBObject, which accepts enumType as a parameter, can only have 2 input values i.e. STRING_ONE or STRING_TWO which will be converted to XML as part of marshalling.
The question is, that what if, I want to handle a situation through my schema design that if I can have an object of enumType then good otherwise I can give an XML to JAXBObject instead of enumType and it still parses it.
From the XSD point of view, it sounds like you want to be able to write
<xs:simpleType name="soft-enumeration">
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="STRING_ONE"/>
<xs:enumeration value="STRING_TWO"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:union memberTypes="xs:string"/>
</xs:simpleType>
</xs:union>
</xs:simpleType>
This type includes the enumeration you describe, but it then unions it with xs:string, so as to accept other values as well. If your schema interface gives you access to information about which member type of the union was used, then you can use that information to treat instances of STRING_ONE and STRING_TWO differently from other strings.
I have no idea whether jaxb does anything useful with this idiom, however; there you're on your own.

how to provide unit values with mininclusive and maxinclusive values

I have one DTD
<parameter name="ReferenceSignalPower" access="readWrite">
<syntax>
<int>
<range minInclusive="-60" maxInclusive="50" />
<units value="dBm">
</units>
</int>
</syntax>
</parameter>
I am new to XML schema i am not aware how to provide unit values with this XML schema
<xs:element name="ReferenceSignalPower">
<xs:simpleType>
<xs:restriction base="xs:unsignedInt">
<xs:minInclusive value="-60"/>
<xs:maxInclusive value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
XML schemas do not have a concept of measurement units, you can define an XML attribute or XML element to contain numbers with certain restrictions (or other standard data-types like dates etc.), but it is the responsability of the application that reads the XML to interpret such numbers as values in a specific unit of measure.
If you want to add measurement unit information in the schema you can do it using the appInfo element - e.g.:
<xs:element name="ReferenceSignalPower">
<xs:simpleType>
<xs:annotation>
<xs:appinfo>
<units value="dBm"/>
</xs:appinfo>
</xs:annotation>
<xs:restriction base="xs:int">
<xs:minInclusive value="-60"/>
<xs:maxInclusive value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
but then you'll have to parse and interpret this information yourself.
You don't say what your question is, but I suppose the question is "why does this not work?"
Your declaration is almost right, but not quite. Why are you using unsignedInt as your base type, when you want a minimum value of -60? The minimum value of unsignedInt is already set to zero.
The XSD spec assumes that if you attempt to set a minimum value to a value that's not part of the value space of your type, then there is an error somewhere.
So: either use a signed integer type as your base type, or set the minimum value to a value that is actually present in the base type.

XML Schema pattern which allows no data at all

I'm receiving xml data in these two forms from an external company
<currencydate>20110910</currencydate>
<currencydate/>
I want to verify using a pattern that this date indeed has the format YYYYMMDD like this
<xs:element name="currencydate" type="dateType"/>
<xs:simpleType name="dateType">
<xs:restriction base="xs:string">
<xs:pattern value="[0-2][0-9]{3}[0-1][0-9][0-3][0-9]"/>
</xs:restriction>
</xs:simpleType>
This works fine. But the validation breaks on the empty element
So I added the minOccurs like this
<xs:element name="currencydate" type="dateType" minOccurs="0"/>
No success so I added nillable
<xs:element name="currencydate" nillable="true" type="dateType" minOccurs="0"/>
No success, I guess the element is there so it checks the pattern. So I changed the pattern
<xs:pattern value="[0-2][0-9]{3}[0-1][0-9][0-3][0-9]|"/>
I only added the pipe indicating the value can be empty. But still no success.
So my question is: how can I check the data pattern but also allow the value
<currencydate/>
Please note I'm receiving this data from an external company which does not provide an xsd nor are they willing to change anything for me.
Did you already try
<xs:pattern value="|([0-2][0-9]{3}[0-1][0-9][0-3][0-9])" />
as suggested in how to validate empty string value tag in xsd?
I only tried it in VS 2010 Express but it seems to work even if a comment in the linked post tells otherwise.

How to allow typed values to be empty with an XML schema?

I have some XML documents over which I have no control whatsoever. Their structure is well-defined, but it is described in a bunch of PDFs, which, despite being very exact, don't make automated validation very tractable. I'm trying to write a XML schema to make (most of) the rules in those PDFs executable.
All the elements are mandatory. But about half of them can be either empty or have simple typed content.
When defining datatypes for these elements, I defined two versions of each: a "normal" one, and another that can be empty. I did this by defining unions with an empty datatype:
<xs:simpleType name="empty">
<xs:restriction base="xs:string">
<xs:length value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="codPostal">
<xs:restriction base="xs:string">
<xs:pattern value="^[0-9]{4}-[0-9]{3}$"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="opt_codPostal">
<xs:union memberTypes="empty codPostal"/>
</xs:simpleType>
Is there a less repetitive way of doing this?
You can use xs:nillable.
In XSD
<xs:simpleType name="codPostal">
<xs:restriction base="xs:string">
<xs:pattern value="^[0-9]{4}-[0-9]{3}$"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="OptionalString" type="codPostal" nillable="true" />
In Document
<OptionalString xsi:nil="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
This is most useful for non-string types (e.g. datetime etc) as for strings you could just use zero length.
<OptionalString />
Unfortunately you need to specify the "nil" attribute on the document. As far as I know, the only non-intrusive way to do what you want is the union type approach that you've already chosen.

Resources