Monday, 28 November 2016

Understand "elementFormDefault" and "attributeFormDefault"

elementFormDefault="qualified"
 
Here, i am providing an example to create an xsd with 
elementFormDefault="qualified"
NOTE: observe qualifiers in xml (blue color text).
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
               targetNamespace="http://example.org/calcSchemaDoc"
               xmlns:tns="http://aimtech.org/calcSchemaDoc"
               elementFormDefault="qualified">
  
      <element name="response">           
            <complexType>
            <sequence>       
                  <element name="fieldOne" type="integer"/> 
                  <element name="fieldTwo" type="integer"/> 
                  <element name="result" type="integer"/> 
                  <element name="typeOfOperation" type="string"/>
            </sequence> 
            </complexType>     
      </element>
</schema>

By Following above XSD if we create a valid XML, then it looks as below
valid XML:
<
ns:response xmlns:ns="http://example.org/calcSchemaDoc">
      <ns:fieldOne>100</
ns:fieldOne>
      <
ns:fieldTwo>200</ns:fieldTwo>
      <
ns:result>300</ns:result>
      <
ns:typeOfOperation>add</ns:typeOfOperation>
</
ns:response>

XPath Requirement:
a) create Xpath to get the value of "fieldTwo" element.
XPath: /ns:response/ns:result/text()
result: 300
*****************************************************************************************************
elementFormDefault="unqualified"


Here, i am providing an example to create an xsd with elementFormDefault="unqualified" 

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://aimtech.org/calcSchemaDoc"
              xmlns:tns="http://example.org/calcSchemaDoc"
              elementFormDefault="unqualified">

      <element name="response">
            <complexType>
            <sequence> 
                  <element name="fieldOne" type="integer"/>
                  <element name="fieldTwo" type="integer"/> 
                  <element name="result" type="integer"/> 
                  <element name="typeOfOperation" type="string"/> 
             </sequence> 
            </complexType>      </element>
</schema>


valid XML:

<?xml version="1.0" encoding="UTF-8"?> 
<ns:response xmlns:ns="http://example.org/calcSchemaDoc">
      <fieldOne>100</fieldOne>
      <fieldTwo>200</fieldTwo>
      <result>300</result>
      <typeOfOperation>add</typeOfOperation>
</ns:response>
NOTE: No need to use qualifiers in xml sub elements, because as per XSD we set elementFormDefault="unqualified"
 
XPath Requirement:

a) create Xpath to get the value of "fieldTwo" element.
XPath: /ns:response/result/text()
result: 300

No comments:

Post a Comment