Skip to content

XMLSchemata

do- edited this page Oct 27, 2024 · 25 revisions

XMLSchemata is a class representing a complete set of XMLSchema instances, with all imports resolved.

More precisely, it's a Map of target namespace URIs to corresponding XMLSchema objects, with some additional fields and methods.

The sole purpose of this class is to produce XMLMarshaller instances for translating data objects into valid XML documents according to a given XML Schema.

const xs = new XMLSchemata ('wsdl_related_stuff.xsd')

const m = xs.createMarshaller (
  localName 
  , namespaceURI
  , {space: 2} // see XMLPrinter for more
)

const xml = m.stringify (data)

Validating existing XML content is not supported, as of yet.

Constructor

const xs = new XMLSchemata ('wsdl_related_stuff.xsd') // or some.wsdl

This loads XML schemata starting with the provided file and then hierarchically by import elements.

Limitations

  • import is supported, but include is not yet;
  • only relative paths are supported as schemaLocations.

Instance Methods

createMarshaller

This method creates an instance of XMLMarshaller corresponding to the complex type with the mentioned localName and namespaceURI. That object is ready to serialize data to XML according to that schema fragment.

stringify

This method does exactly the opposite to XMLNode.toObject but somewhat analogous to JSON.stringify: it serializes a plain object to XML according to the schema[ta] loaded.

The input object must consist of exactly one entry with the key corresponding to the local name of some complex type:

const xs = new XMLSchemata ('wsdl_related_stuff.xsd')

const xml = xs.stringify ({
  MySoapRequest: {
    id: 1,
    amount: 1000,
    //...
  }
})

Actually, the serialization is done by XMLMarshaller -- see the corresponding page for details, e. g. on current limitations.

XMLSchemata's stringify method is only a convenience wrapper method creating a temporary XMLMarshaller guessing (if possible) the schema by the root element name and using it.

Static Methods

any

Wraps a pair of arguments in a special object to fill in xs:any wildcards during stringify.

xml and attr may be passed as two separate arguments or as a 2-element Array.

const xml = '<someRequest>CODE</someRequest>'
const attr = {Id: 1}

const Envelope = {Body: XMLSchemata.any (xml, attr)}
  // = {null: {"<someRequest>CODE</someRequest>": {Id: 1}}}

const Envelope = {Body: XMLSchemata.any ([xml, attr])}
  // same result

Parameters

xml

A raw String to be injected by stringify as any's content without any additional processing (e. g. escaping unsafe characters).

attr

An object to be injected as anyAttribute's attribute-like key/value pairs: real attributes, or maybe namespace definitions. Values are not processed, neither are keys, so the latter may contain raw namespace prefixes.

This parameter is optional.

Some Details

The [Symbol ('_index')] field contains the map of all named schema objects to Sets of corresponding target namespaces. Normally, each of such sets must contain only one element. In this case, it's possible to unambiguously detect the schema by only a local element name (like MyVerySpecialRequestType).

Clone this wiki locally