Tuesday 3 May 2016

elementFormDefault attribute in XSD Schema definition

This attribute (elementFormDefault) is useful when we are defining our own <complexType> in our schema and the main purpose is to decide whether the <element> inside <complexType> should be in targetNamespace or global namespace

Example:
Sample XSD:
<?xml version = '1.0' encoding = 'UTF-8'?>
<xsd:schema xmlns:ns0="http://ticket.com/ns" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ticket.com/ns">  
   <xsd:complexType name="routeTicket">
      <xsd:sequence>
         <xsd:element name="ticket" type="xsd:string" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="routeTicketResponse">
      <xsd:sequence>
         <xsd:element name="return" type="xsd:string" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="routeTicket" type="ns0:routeTicket"/>
   <xsd:element name="routeTicketResponse" type="ns0:routeTicketResponse"/>

</xsd:schema>


when elementFormDefault="unqualified"
when elementFormDefault="qualified"
Valid XML for the given XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:routeTicket xmlns:xs="http://ticket.com/ns">
  <ticket>str1234</ticket>
</xs:routeTicket>

Valid XML for the given XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:routeTicket xmlns:xs="http://ticket.com/ns">
  <xs:ticket>str1234</xs:ticket>
</xs:routeTicket>

XPATH to access ticket

/xs:routeTicket/ticket

XPATH to access ticket

/xs:routeTicket/xs:ticket


Useful online tools

No comments:

Post a Comment