Skip to content

XSSimpleType

do- edited this page Dec 20, 2024 · 41 revisions

XSSimpleType is a class which instances represent XML Schema's simple type definitions.

Some of them, the predefined ones, correspond to built in datatypes. Others are constructed while loading XMLSchemata. Each derived XML type is modelled by a single instance of a class extending the parent representation. All those are internal objects mostly used by XMLMarshaller.stringify ().

The main method, stringify (value), maps an arbitrary non-nullish value into the corresponding string from the type's lexical space, or throws an error when the argument is unacceptable. In the base implementation, it simply calls toString(). This is a fallback solution for exotic XML Schema types like gYearMonth not having any natural counterpart in javaScript.

Specific ancestor classes do some necessary validation and conversion for numbers, dates and so on. Here is the reference on the class family:

Subclass XML Types Acceptable Values
XSSimpleTypeBoolean boolean true, false, 'true', 'false', 1, 0, '1', '0'
XSSimpleTypeDate date String (ISO), Date() constructor argument
XSSimpleTypeDateTime dateTime String (ISO), Date() constructor argument
XSSimpleTypeDecimal decimal ! isNaN()
XSSimpleTypeInteger integer Number.isInteger()
XSSimpleTypeFloat double, float ! isNaN()
XSSimpleTypeQName QName {localName, namespaceURI}
XSSimpleType all other non-nullish

Limitations

In general, the output is not guaranteed to be valid for any possible incoming value.

There is a certain functionality/performance trade-off here. The application developer is expected to supply some reasonable content.

Date & Time

XSSimpleTypeDate and XSSimpleTypeDateTime receive either a string or any other Date () parameter, including a Date instance.

Input strings are mentioned separately because they are considered preformatted according to XML standards (which comply to the widely supported ISO 8601 specification). In this case, no Date object is constructed to be serialized back, but, for the sake of performance, the value is mostly passed through, optionally trimmed down.

For instance, XSSimpleTypeDate leaves any 10 char long string as is, without spending a CPU cycle on checking for YYYY-MM-DD format, month number, leap year etc. It's OK to supply a dateTime value for a date placeholder, the time part will be truncated based on length only, but xml-toolkit will never try to fix any random content.

fractionDigits

For simple types derived from decimal by applying restrictions, the last fractionDigits facet is used. For instance, for a type restricted with

<fractionDigits value='2'/>

the output will be calculated with toFixed (2).

pattern

Each XSSimpleType instance keeps the list of RegExps mentioned in all pattern restricting facets up by the inheritance tree. The stringify (value) method scans through possible textual representations and returns the first one satisfying all restrictions.

For instance, the base XSSimpleTypeBoolean outputs true as the canonical true, but its ancestor generated for SOAP 1.1's mustUnderstand attribute

<xs:simpleType>
  <xs:restriction base="xs:boolean">
    <xs:pattern value="0|1"/>
  </xs:restriction>
</xs:simpleType>

prints 1 instead.

Clone this wiki locally