<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dimuthu's Blog &#187; xml schema</title>
	<atom:link href="http://www.dimuthu.org/tag/xml-schema/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dimuthu.org</link>
	<description>Waiting for your comments</description>
	<lastBuildDate>Wed, 21 Dec 2011 05:39:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
  <link>http://www.dimuthu.org</link>
  <url>http://www.dimuthu.org/favicon.ico</url>
  <title>Dimuthu's Blog</title>
</image>
		<item>
		<title>XML Schema Simple Types &amp; How WSDL2PHP Convert Them To PHP</title>
		<link>http://www.dimuthu.org/blog/2008/11/13/xml-schema-simple-types-how-wsdl2php-convert-them-to-php/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/13/xml-schema-simple-types-how-wsdl2php-convert-them-to-php/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 15:28:50 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml schema]]></category>
		<category><![CDATA[built-in]]></category>
		<category><![CDATA[facets]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[primitive]]></category>
		<category><![CDATA[simple types]]></category>
		<category><![CDATA[union]]></category>
		<category><![CDATA[wsdl2php]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=598</guid>
		<description><![CDATA[As many of other languages, XML schema too have data types. Basically it can be categorized in to two groups. Simple Types Complex Types The different between these 2 types are so easy to identify. Say you have a schema element with a simple type like this. &#60;xs:element name="xx" type="someSimpleType"/&#62; Then a possible xml to [...]]]></description>
			<content:encoded><![CDATA[<p>As many of other languages, XML schema too have data types. Basically it can be categorized in to two groups.</p>
<ol>
<li>Simple Types</li>
<li>Complex Types</li>
</ol>
<p>The different between these 2 types are so easy to identify. Say you have a schema element with a simple type like this.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"xx"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"someSimpleType"</span><span style="font-weight: bold; color: black;">/&gt;</span></span></pre>
<p>Then a possible xml to validate against this schema is</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xx<span style="font-weight: bold; color: black;">&gt;</span></span></span>Bingo<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/x<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Inside the &#8216;xxx&#8217; you can only find texts, But in a case of a  schema element with complex types,</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"xx"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"someComplexType"</span><span style="font-weight: bold; color: black;">/&gt;</span></span></pre>
<p>The XML will be set of nested elements.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xx<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;yy<span style="font-weight: bold; color: black;">&gt;</span></span></span>hi<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/yy<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;zz<span style="font-weight: bold; color: black;">&gt;</span></span></span>Nested elements<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/zz<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xx<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Simply complex type contains elements with simple types or complex types similar to the class types in OOP languages which contain member variables with different types.</p>
<p>But unlike most of the other languages, in schema simple types are not always primitive types. There can be user derived simple types as well.</p>
<p>I will talk about each of these variations in schema types and their PHP representation of <a href="http://labs.wso2.org/wsf/php/wsdl2phptool.php">WSDL2PHP tool</a>.</p>
<ul>
<li>Primitive Types &amp; Built-in Types</li>
<li>List Types</li>
<li>Union Types</li>
<li>Faceted Types</li>
</ul>
<p><strong>Primitive Types &amp; Built-in Types<br />
</strong></p>
<p>The XML schema specification talks about <a href="http://www.w3.org/TR/xmlschema-2/#built-in-primitive-datatypes">19 primitive types</a>. Some of the common of these simple types and the corresponding PHP types generated by WSDL2PHP tool are,</p>
<table border="1">
<tbody>
<tr style="background:#dddddd">
<td>Schema Type</td>
<td>PHP Type</td>
</tr>
<tr>
<td><a href="http://www.w3.org/TR/xmlschema-2/#string">string</a></td>
<td>string</td>
</tr>
<tr>
<td><a href="http://www.w3.org/TR/xmlschema-2/#boolean">boolean</a></td>
<td>boolean</td>
</tr>
<tr>
<td><a href="http://www.w3.org/TR/xmlschema-2/#decimal">decimal</a></td>
<td>float</td>
</tr>
<tr>
<td><a href="http://www.w3.org/TR/xmlschema-2/#float">float</a></td>
<td>float</td>
</tr>
<tr>
<td><a href="http://www.w3.org/TR/xmlschema-2/#double">double</a></td>
<td>double</td>
</tr>
<tr>
<td><a href="http://www.w3.org/TR/xmlschema-2/#duration">duration</a></td>
<td>string</td>
</tr>
<tr>
<td><a href="http://www.w3.org/TR/xmlschema-2/#dateTime">dateTime</a></td>
<td>string (from above wsf/php 2.0 it is an integer &#8211; representing timestamp)</td>
</tr>
</tbody>
</table>
<p>You may have noticed the famous types like &#8216;integer&#8217;, &#8216;byte&#8217;, &#8216;short&#8217; are missing out of the above list. In fact these types exist, but they are not primitive types. They are derived from decimal (special cases when the fraction digits are 0 <img src='http://www.dimuthu.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Anyway still these types are considered built-in types in xml schema. Here is the <a href="http://www.w3.org/TR/xmlschema-2/#built-in-primitive-datatypes">complete list of built-in types</a> (both primitive and non-primitive built-in types)</p>
<p>&#8216;integer&#8217;, &#8216;byte&#8217; and &#8216;short&#8217; are mapped to PHP integer types by the WSDL2PHP tool.</p>
<p>To show an example how WSDL2PHP tool generating code for simple types, I will take the following schema Element.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"myName"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:string"</span><span style="font-weight: bold; color: black;">/&gt;</span></span></pre>
<p>And WSDL2PHP will generate a simple public variable (Assuming this is generating inside some class) for that element with a comment to describing it.</p>
<pre class="php">    <span style="font-style: italic; color: #808080;">/**
     * @var string
     */</span>
     <span style="font-weight: bold; color: #000000;">public</span> <span style="color: #0000ff;">$myName</span>;</pre>
<p>So if I specified some value for the variable (Taking the variable name of the parent class is $parent),</p>
<pre class="php"><span style="color: #0000ff;">$parent</span>-&gt;<span style="color: #006600;">myName</span> = <span style="color: #ff0000;">"James"</span>;</pre>
<p>I will get the following nice XML.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;myName<span style="font-weight: bold; color: black;">&gt;</span></span></span>James<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/myName<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>This is the same approach for all the other built-in types as well.</p>
<p><strong>List Types</strong></p>
<p>The list types are derived from list of simple types. It still a simple type because it list all the element in the list as a white-space separated text inside the parent element.</p>
<p>Here is how you may declare a list type in an xml schema.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:simpleType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"mylist"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:list</span> <span style="color: #000066;">itemType</span>=<span style="color: #ff0000;">"xs:int"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:simpleType<span style="font-weight: bold; color: black;">&gt;</span></span></span>

<span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- example element to have the above list value --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"xCoordinates"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"tns:mylist"</span><span style="font-weight: bold; color: black;">/&gt;</span></span></pre>
<p>Then the WSDL2PHP generated PHP code would be something like,</p>
<pre class="php">    <span style="font-style: italic; color: #808080;">/**
     * @var array of int
     */</span>
    <span style="font-weight: bold; color: #000000;">public</span> <span style="color: #0000ff;">$xCoordinates</span>;</pre>
<p>As the comment implies, you have to feed the values in an php array.</p>
<pre class="xml"><span style="color: #0000ff;">$parent</span>-&gt;<span style="color: #006600;">xCoordinates</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">7</span>, <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">)</span>;</pre>
<p>And Here is the XML you are getting,</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xCoordinates<span style="font-weight: bold; color: black;">&gt;</span></span></span>1 3 8 7 9<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xCoordinates<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Similarly you can have list of any simple types regardless whether it is built-in or derived.</p>
<p><strong>Union Types</strong></p>
<p>The union in schema is similar to the unions in &#8216;C&#8217;. In C variables of a union can have one (and not more than one) of the types declared inside the union type. Similarly in schema elements with union types can have one and only one simple type among the member types declared in the union. Here is an example of declaring union type,</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:simpleType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"myIntStringUnion"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:union</span> <span style="color: #000066;">memberTypes</span>=<span style="color: #ff0000;">"xs:int xs:string"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:simpleType<span style="font-weight: bold; color: black;">&gt;</span></span></span>

<span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- example element to have the above union value --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"garbage"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"tns:myIntStringUnion"</span><span style="font-weight: bold; color: black;">/&gt;</span></span></pre>
<p>And the WSDl2PHP generated code,</p>
<pre class="php">    <span style="font-style: italic; color: #808080;">/**
     * @var int/string
     */</span>
    <span style="font-weight: bold; color: #000000;">public</span> <span style="color: #0000ff;">$garbage</span>;</pre>
<p>So if you read the comment you can have an idea that this variable accept either &#8216;in&#8217; or &#8216;string&#8217; type without digging in to the WSDL.</p>
<p><strong>Faceted Types</strong></p>
<p>You can apply facets and restrict the value space of a simple type.</p>
<ul>
<li>length</li>
<li>minLength</li>
<li>maxLength</li>
<li>pattern</li>
<li>enumeration</li>
<li>whiteSpace</li>
<li>maxInclusive</li>
<li>maxExclusive</li>
<li>minExclusive</li>
<li>minInclusive</li>
<li>totalDigits</li>
<li>fractionDigits</li>
</ul>
<p>Some facets are specific to some built-in data types or types derived from these built-in datatypes. <a href="http://www.w3.org/TR/xmlschema-2/#built-in-primitive-datatypes">Here is a table of valid facets</a> for each of the above mentioned catagory of types.</p>
<p>I have earlier blogged about how you work on faceted types in PHP in the post &#8220;<a href="http://www.dimuthu.org/blog/2008/10/21/coding-schema-inheritance-in-php/">Coding Schema Inheritance in PHP</a>&#8220;. So Here I will list out a similar example  to demonstrate the PHP generated code by WSDL2PHP tool for facets.</p>
<p>Here is a schema with the &#8216;enumeration&#8217; facet.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- applying enumeration facet to the xs:string --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:simpleType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"mySubject"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:restriction</span> <span style="color: #000066;">base</span>=<span style="color: #ff0000;">"xs:string"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
         <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:enumeration</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"Maths"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
         <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:enumeration</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"Physics"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
         <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:enumeration</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"Chemistry"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:restriction<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:simpleType<span style="font-weight: bold; color: black;">&gt;</span></span></span>

<span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- example element to have the above faceted value --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"subject"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"tns:mySubject"</span><span style="font-weight: bold; color: black;">/&gt;</span></span></pre>
<p>And here is the WSDL2PHP generated code,</p>
<pre class="php">    <span style="font-style: italic; color: #808080;">/**
     * @var string
     *     NOTE: subject should follow the following restrictions
     *     You can have one of the following value
     *     Maths
     *     Physics
     *     Chemistry
     */</span>
    <span style="font-weight: bold; color: #000000;">public</span> <span style="color: #0000ff;">$subject</span>;</pre>
<p>It clearly says your variable &#8216;subject&#8217; is only allowed to have the list of values mentioned in the &#8216;enumeration&#8217;.</p>
<p>So I just wrote about some variations of simple schema types and how they are represented in the WSDL2PHP generated code. As you may have observed, you don&#8217;t need to know any thing about the WSDL or the schema to write a PHP code around that, since WSDL2PHP gives you a generated code mentioning all the guidelines, restrictions about using them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/13/xml-schema-simple-types-how-wsdl2php-convert-them-to-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>WSF/PHP WSDL Mode &#8211; Handling XML Schema Arrays</title>
		<link>http://www.dimuthu.org/blog/2008/10/25/wsfphp-wsdl-mode-handling-xml-schema-arrays/</link>
		<comments>http://www.dimuthu.org/blog/2008/10/25/wsfphp-wsdl-mode-handling-xml-schema-arrays/#comments</comments>
		<pubDate>Sat, 25 Oct 2008 15:20:40 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml schema]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[maxOccurs]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[wsdl mode]]></category>
		<category><![CDATA[wsdl2php]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=543</guid>
		<description><![CDATA[In XML Schema we declare an array or a multiple occurrence of a schema element by setting its maxOccurs attribute to a value greater than 1 or to the value &#8220;unbounded&#8221; in a case of no maximum boundary. &#60;xs:element maxOccurs="unbounded"  minOccurs="0"  name="params"  nillable="true"  type="xs:int"/&#62; If you generate PHP code to such a schema using wsdl2php [...]]]></description>
			<content:encoded><![CDATA[<p>In XML Schema we declare an array or a multiple occurrence of a schema element by setting its maxOccurs attribute to a value greater than 1 or to the value &#8220;unbounded&#8221; in a case of no maximum boundary.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">maxOccurs</span>=<span style="color: #ff0000;">"unbounded"
           </span> <span style="color: #000066;">minOccurs</span>=<span style="color: #ff0000;">"0"</span>
            <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"params"</span>
            <span style="color: #000066;">nillable</span>=<span style="color: #ff0000;">"true"</span>
            <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:int"</span><span style="font-weight: bold; color: black;">/&gt;</span></span></pre>
<p>If you generate PHP code to such a schema using <a title="online wsdl2php tool" href="http://labs.wso2.org/wsf/php/wsdl2phptool.php">wsdl2php tool</a>, you will get a code for the class variable named &#8220;params&#8221; similar to this.</p>
<pre class="php">    <span style="font-style: italic; color: #808080;">/**
     * @var array[0, unbounded] of int
     */</span>
    <span style="font-weight: bold; color: #000000;">public</span> <span style="color: #0000ff;">$params</span>;</pre>
<p>So if you have a variable (say $object) for the object of this class you can fill the &#8220;params&#8221; field like this,</p>
<pre class="php"><span style="color: #0000ff;">$object</span>-&gt;<span style="color: #006600;">params</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">)</span>;</pre>
<p>This will create the xml with the expected array of elements.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wrapper<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;params<span style="font-weight: bold; color: black;">&gt;</span></span></span>1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/params<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;params<span style="font-weight: bold; color: black;">&gt;</span></span></span>5<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/params<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;params<span style="font-weight: bold; color: black;">&gt;</span></span></span>8<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/params<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wrapper<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Not only for simple types, you can have arrays of complex types too.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">maxOccurs</span>=<span style="color: #ff0000;">"unbounded"</span>
            <span style="color: #000066;">minOccurs</span>=<span style="color: #ff0000;">"0"</span>
            <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"params"</span>
            <span style="color: #000066;">nillable</span>=<span style="color: #ff0000;">"true"</span>
            <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"tns:MyComplexType"</span><span style="font-weight: bold; color: black;">/&gt;
</span></span></pre>
<p>Instead of giving simple integers like earlier case, this time you will feed the &#8220;params&#8221; variable with an array of PHP objects of &#8216;MyComplexType&#8217; class.</p>
<pre class="php"><span style="color: #0000ff;">$obj1</span> = <span style="font-weight: bold; color: #000000;">new</span> MyComplexType<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="font-style: italic; color: #808080;">/* feeding data to obj1 variables */</span>

<span style="color: #0000ff;">$obj2</span> = <span style="font-weight: bold; color: #000000;">new</span> MyComplexType<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="font-style: italic; color: #808080;">/* feeding data to obj2 variables */</span>

<span style="color: #0000ff;">$obj3</span> = <span style="font-weight: bold; color: #000000;">new</span> MyComplexType<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="font-style: italic; color: #808080;">/* feeding data to obj3 variables */</span>

<span style="color: #0000ff;">$object</span>-&gt;<span style="color: #006600;">params</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$obj1</span>, <span style="color: #0000ff;">$obj2</span>, <span style="color: #0000ff;">$obj3</span><span style="color: #66cc66;">)</span>;</pre>
<p>This will create a xml containing array of params similar to this,</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;wrapper<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;params<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;elementx</span><span style="font-weight: bold; color: black;">/&gt;</span></span> <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- elements in the MyComplexType type --&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;elementy</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/params<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;params<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        ... <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- elements in the MyComplexType type --&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/params<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;params<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        ... <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- elements in the MyComplexType type --&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/params<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/wrapper<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/10/25/wsfphp-wsdl-mode-handling-xml-schema-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XML Schema nillable=&#8221;true&#8221; vs minOccurs=&#8221;0&#8243;</title>
		<link>http://www.dimuthu.org/blog/2008/08/18/xml-schema-nillabletrue-vs-minoccurs0/</link>
		<comments>http://www.dimuthu.org/blog/2008/08/18/xml-schema-nillabletrue-vs-minoccurs0/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 15:28:18 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml schema]]></category>
		<category><![CDATA[minOccurs]]></category>
		<category><![CDATA[nillable]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=187</guid>
		<description><![CDATA[In a WSDL, XML Schema is the section where it define the message format for each operations, which eventually become the real API that users are interested. And it is the most tricky part of the WSDL. Nowadays there are many tools that you can design and use WSDLs without any needs in knowing the [...]]]></description>
			<content:encoded><![CDATA[<p>In a WSDL, XML Schema is the section where it define the message format for each operations, which eventually become the real API that users are interested. And it is the most tricky part of the WSDL. Nowadays there are many tools that you can design and use WSDLs without any needs in knowing the meaning of a single line of the WSDL. But there are situations that you may find it is better you have some knowledge in XML Schema section and in WSDL overall.<br />
For this post I m taking a simple example of use of nillable=&#8221;true&#8221; and minOccurs=&#8221;0&#8243;. Take the following example.</p>
<pre><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"myelements"</span><span style="font-weight: bold; color: black;">&gt;</span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">     &lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"nonboth"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:string"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">minOccurs</span>=<span style="color: #ff0000;">"0"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"minzero"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:int"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"nilint"</span> <span style="color: #000066;">nillable</span>=<span style="color: #ff0000;">"true"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:int"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"nilstring"</span> <span style="color: #000066;">nillable</span>=<span style="color: #ff0000;">"true"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:string"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">minOccurs</span>=<span style="color: #ff0000;">"0"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"minzeronil"</span> <span style="color: #000066;">nillable</span>=<span style="color: #ff0000;">"true"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xs:string"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:element<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Just ignore the meaning of what nillable and minOccurs attributes for now. You can safely say the following XML is valid for the above Schema.</p>
<pre><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;myelements<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;nonboth<span style="font-weight: bold; color: black;">&gt;</span></span></span>i can't be either nil nor skipped<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;nonboth<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;minzero<span style="font-weight: bold; color: black;">&gt;</span></span></span>3<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;minzero<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;nilint<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;nilint<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;nilstring<span style="font-weight: bold; color: black;">&gt;</span></span></span>i can have null, but i cant skipeed<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/nilstring<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;minzeronil<span style="font-weight: bold; color: black;">&gt;</span></span></span>i can be skipped and have the nil value<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;minzeronil<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/myelements<span style="font-weight: bold; color: black;">&gt;
</span></span></span></pre>
<p>Take the first element &#8216;nonboth&#8217; in the schema, It has not any minOccurs or nillable attribute. By default minOccurs equal to 1 and nillable equal to false. That mean it can&#8217;t have nil value nor it can not be removed from the xml.</p>
<p>Is that making an element nil and removing the element from the XML is same? No. Take the second element in the schema &#8216;minzerostring&#8217;. There you have minOccurs =&#8221;0&#8243; but there are no nillable=&#8221;true&#8221;, mean it is non-nillable. The idea is whenever you don&#8217;t want that element in your xml, you can&#8217;t have the element keeping empty like</p>
<pre>  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;minzero</span></span><span><span><span style="color: #009900;"><span style="font-weight: bold; color: black;"> xsi:nil="true"</span></span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;"><span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;minzero<span style="font-weight: bold; color: black;">&gt;
</span></span></span></pre>
<p>But you can remove the whole element from the XML (since it is minOccurs=0).</p>
<p>The opposite of the above scenario is nillable=&#8221;true&#8221; but minOccurrs != 0. Check the &#8216;nilint&#8217; element in the schema. There you can&#8217;t skip the element &#8216;nilint&#8217;, you have to have the element &lt;nilint/&gt; but it can hold a nil value.</p>
<pre>  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;nilint</span></span><span><span><span style="color: #009900;"><span style="font-weight: bold; color: black;"> xsi:nil="true"</span></span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;"><span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/nilint<span style="font-weight: bold; color: black;">&gt;
</span></span></span></pre>
<p>or simply</p>
<pre>  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;nilint</span></span><span><span><span style="color: #009900;"><span style="font-weight: bold; color: black;"> xsi:nil="true"</span></span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">/<span style="font-weight: bold; color: black;">&gt;
</span></span></span></pre>
<p>Note that the correct way to declare the nil element is,</p>
<pre>  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;nilint xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">/<span style="font-weight: bold; color: black;">&gt;
</span></span></span></pre>
<p>You can understand why when we look at the third element &#8216;nilstring&#8217;. Say you set message the following element</p>
<pre>  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;nilstring</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;"><span style="font-weight: bold; color: black;">&gt;&lt;/nilstring&gt;
</span></span></span></pre>
<p>You can say that this is not nil, this is an empty string. In fact an empty string is nil in some other language, But if we take XML Schema as a language, then for someone to be nil, it have to have the xsi:nil attribute set to &#8220;true&#8221; or &#8220;1&#8243;.</p>
<p>So going back to the &#8216;minzero&#8217; which is non-nillable, by theory you should be able to write the following xml,</p>
<pre>  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;minzero/</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;"><span style="font-weight: bold; color: black;">&gt;
</span></span></span></pre>
<p>Since you don&#8217;t have that xsi:nil=&#8221;1&#8243; this is not a nil value, so the condition nillable=&#8221;false&#8221; condition is preserved. But unlike for string when you set an empty element  for an integer, it doesn&#8217;t sound correct. So in practice whenever some schema says non-nillable you should set some valid value.</p>
<p>The last one is &#8216;minzeronil&#8217; element which is both nillable=&#8221;true&#8221; and minOccurs=&#8221;0&#8243;. Whenever you don&#8217;t need to set a value for this element, you have the choice of either skipping the element or setting the value of the element to nil. It is obvious rather than setting a nil value it is better you just skip the element to make the XML shorter. This is really needed specially in web services where you need the payload to be minimum as much as possible.</p>
<p>Say you have to prepare the XML and you don&#8217;t have valid values for any of the element. So this can be the optimum XML you can create.</p>
<pre><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;myelements<span style="font-weight: bold; color: black;"> </span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">/</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;"><span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;nonboth<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;nonboth<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;nilint xsi:nil="1"/</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;"><span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">nilstring xsi:nil="1"<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/myelements<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Read <a href="http://www.ibm.com/developerworks/webservices/library/ws-tip-null.html">this nice article in developer works on nillable=&#8221;true&#8221; and minOccurs=&#8221;0&#8243;</a> for more.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/08/18/xml-schema-nillabletrue-vs-minoccurs0/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

