user3619960
user3619960

Reputation: 11

How to set a default/fixed value for a complex type element?

<xsd:element name="category">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:extension base="xsd:string">
          <xsd:attribute default="http://schemas.xmlsoap.org/soap/actor/next" ref="soapenv:actor" use="optional"/>
          <xsd:attribute ref="soapenv:mustUnderstand" use="optional"/>
        </xsd:extension>
      </xsd:simpleContent>
    </xsd:complexType>
</xsd:element>

The element name is “category” and I want it to always be defaulted to the value “ABC”

The above definition generates the element in Soap UI as follows:

<urn:category soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="?">?</urn:category> 

What I need is that the element should get generated as

<urn:category soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="?">ABC</urn:category> 

To achieve this, I tried

  1. <xsd:element name="category" fixed="ABC">
  2. <xsd:element name="category" default="ABC">

None of the above was able to generate the element with the value

Am I doing something wrong here? Please help.

Thanks.

How can I achieve this?

Upvotes: 1

Views: 4045

Answers (1)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

From the XSD point of view, the ways you have tried to specify a default or fixed value are the correct ways, and they seem to work fine to me. That is, if I create a schema document containing the element declaration you give, with the additional attribute fixed="ABC" on the xsd:element element, then the document <category>ABC</category> is valid against the schema and the document <category>abc</category> is not, and elicits a complaint about the value not matching the required value 'ABC'.

To be painfully explicit, this is the full schema document I used:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:soapenv="http://example.com/soapenv"
  elementFormDefault="qualified">

  <xsd:import namespace="http://example.com/soapenv"
  schemaLocation="complex-default.2.xsd"/>

  <xsd:element name="category" fixed="ABC">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:extension base="xsd:string">
          <xsd:attribute 
            default="http://schemas.xmlsoap.org/soap/actor/next"
            ref="soapenv:actor" 
            use="optional"/>
          <xsd:attribute ref="soapenv:mustUnderstand" 
            use="optional"/>
        </xsd:extension>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>

</xsd:schema>

Note that the namespace name for the soapenv prefix is one I made up; the schema document complex-default.2.xsd referred to for that namespace is a simple placeholder designed to supply unconstraining definitions of the two attributes in the namespace used in your schema fragment:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://example.com/soapenv"
           elementFormDefault="qualified"> 
  <xs:attribute name="actor" type="xs:anyURI"/>
  <xs:attribute name="mustUnderstand" type="xs:boolean"/>
</xs:schema>

Perhaps the problem is with your SOAP tool, and not with your XSD schema document.

Upvotes: 1

Related Questions