Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define xsi:type as an attribute in XML-schema?

Tags:

xml

xsd

I have an XML for which I want to write a schema definition. The problem is that I don't know how to define xsi:type as an attribute. Here is the XML element:

<SerializedData xsi:type="xs:double">300.0</SerializedData>

My XML-schema definition so far looks like this:

<complexType name="SerializedDataType">
    <simpleContent>
        <extension base="double">

        </extension>
    </simpleContent>
</complexType>

I have also tried defining it like Ian Roberts suggested:

<element name="SerializedData"/>

However, when I use BPEL designer to initialize it like this:

<SerializedData xsi:type="xs:double">300.0</SerializedData>

I get the following warning:

The fixed value you entered does not appear to be valid XML (which is required for some types of fixed values to work correctly). It will be saved in a text format.

If I initialize it like this there is no warning:

<SerializedData>300.0</SerializedData>

But the problem is that the Web Service I am trying to invoke expects the request SOAP message to include the attribute xsi:type="xs:double". How can I make my SOAP request message to include it?

Any help would be greatly appreciated!

like image 959
Peter Avatar asked Jan 19 '14 12:01

Peter


People also ask

What is XSI type in XML?

The prefix "xsi" is the namespace prefix used by convention for the XML Schema instance namespace. XML documents can contain elements that have an xsi:type attribute. This behavior provides an explicit data type for the element. The MRM XML parser in sensitive to xsi:type attributes in the XML document.

How do you specify type in XML?

Typing an Element in the XML Document An element can also be typed through the dt attribute on the instance of the element. To do this, declare the data type namespace at the beginning of the XML document. The dt prefix can now be used on the dt attribute to assign a data type to an instance of an element.

Can we define a data type for an element in XML Schema?

It can be one of the types included in the XML Schema definition (boolean, string, date, etc.), or it can be a custom type that you can define yourself. You can also add restrictions (facets) to a data type in order to limit its content, or you can require the data to match a specific pattern.

What is type attribute in XML?

Attribute Types This means, any string of non-markup characters is a legal part of the attribute. ID − It is used to specify the element as unique. IDREF − It is used to reference an ID that has been named for another element. IDREFS − It is used to reference all IDs of an element.


2 Answers

The xsi:type attribute doesn't need to be declared in the schema: it's implicitly declared, and can be used on any element. But in an instance, it has to be a valid QName.

If you write

<SerializedData xsi:type="xs:double">300.0</SerializedData>

then

(a) to be namespace-well-formed, you need to declare the "xsi" namespace

(b) to be schema-valid, you also need to declare the "xs" namespace.

As Roberts indicated, this means you should write

<SerializedData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xsi:type="xs:double">300.0</SerializedData>
like image 59
Michael Kay Avatar answered Nov 09 '22 00:11

Michael Kay


You don't need to - just declare the element without a type at all.

<element name="SerializedData" />

The xsi:type attribute is used to indicate to the schema validator that the real type of a particular instance of an element is not the element's declared type but rather a sub-type derived from the declared type. By declaring the element with no type you are saying it can have any type, and you will use xsi:type in the instance to specify which.

Strictly you're declaring an element whose type is the "ur-type" which is the root of the XML Schema type hierarchy - all types, simple and complex, ultimately derive from the ur-type. If you want to restrict the SerializedData element to simple content only (no sub-elements or attributes) then declare it as

<element name="SerializedData" type="anySimpleType" />

Regarding the second part of your question, your designer tool is right that in isolation

<SerializedData xsi:type="xs:double">300.0</SerializedData>

is not correct XML, because the xsi namespace has not been declared. Try adding the namespace declarations:

<SerializedData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xsi:type="xs:double">300.0</SerializedData>
like image 31
Ian Roberts Avatar answered Nov 08 '22 22:11

Ian Roberts