<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Examples</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sdo-das-xml.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.sdo-das-xml.html">SDO DAS XML Functions</a></div> <div class="up"><a href="book.sdo-das-xml.html">SDO DAS XML</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="sdo-das-xml.examples" class="chapter"> <h1>Examples</h1> <p class="para"> Several of the following examples are based on the <a href="sdo.sample.sequence.html" class="link">letter example</a> described in the <a href="ref.sdo.html" class="link">SDO documentation</a>. The examples assume the XML Schema for the letter is contained in a file <var class="filename">letter.xsd</var> and the letter instance is in the file <var class="filename">letter.xml</var>. These two files are reproduced here: </p> <p class="para"> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode"><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:letter="http://letterSchema" targetNamespace="http://letterSchema"> <xsd:element name="letters" type="letter:FormLetter"/> <xsd:complexType name="FormLetter" mixed="true"> <xsd:sequence> <xsd:element name="date" minOccurs="0" type="xsd:string"/> <xsd:element name="firstName" minOccurs="0" type="xsd:string"/> <xsd:element name="lastName" minOccurs="0" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema></pre> </div> </div> </p> <p class="para"> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode"><letter:letters xmlns:letter="http://letterSchema"> <date>March 1, 2005</date> Mutual of Omaha Wild Kingdom, USA Dear <firstName>Casy</firstName> <lastName>Crocodile</lastName> Please buy more shark repellent. Your premium is past due. </letter:letters></pre> </div> </div> </p> <div class="example" id="example-6697"> <p><strong>Example #1 Loading, altering, and saving an XML document</strong></p> <div class="example-contents"><p> The following example shows how an XML document can be loaded from a file, altered, and written back. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**<br /> * Load, update, and save an XML document<br /> */<br /></span><span style="color: #007700">try {<br /> </span><span style="color: #0000BB">$xmldas </span><span style="color: #007700">= </span><span style="color: #0000BB">SDO_DAS_XML</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">"letter.xsd"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$document </span><span style="color: #007700">= </span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">loadFile</span><span style="color: #007700">(</span><span style="color: #DD0000">"letter.xml"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$root_data_object </span><span style="color: #007700">= </span><span style="color: #0000BB">$document</span><span style="color: #007700">-></span><span style="color: #0000BB">getRootDataObject</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">$root_data_object</span><span style="color: #007700">-></span><span style="color: #0000BB">date </span><span style="color: #007700">= </span><span style="color: #DD0000">"September 03, 2004"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$root_data_object</span><span style="color: #007700">-></span><span style="color: #0000BB">firstName </span><span style="color: #007700">= </span><span style="color: #DD0000">"Anantoju"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$root_data_object</span><span style="color: #007700">-></span><span style="color: #0000BB">lastName </span><span style="color: #007700">= </span><span style="color: #DD0000">"Madhu"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">saveFile</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">, </span><span style="color: #DD0000">"letter-out.xml"</span><span style="color: #007700">);<br /> echo </span><span style="color: #DD0000">"New file has been written:\n"</span><span style="color: #007700">;<br /> print </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">"letter-out.xml"</span><span style="color: #007700">);<br />} catch (</span><span style="color: #0000BB">SDO_Exception $e</span><span style="color: #007700">) {<br /> print(</span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p> An instance of the XML DAS is first obtained from the <span class="function"><a href="sdo-das-xml.create.html" class="function">SDO_DAS_XML::create()</a></span> method, which is a static method of the <strong class="classname">SDO_DAS_XML</strong> class. The location of the xsd is passed as a parameter. Once we have an instance of the XML DAS initialised with a given schema, we can use it to load the instance document using the <span class="function"><strong>loadFile()</strong></span> method. There is also a <span class="function"><strong>loadString()</strong></span> method if you want to load an XML instance document from a string. If the instance document loads successfully, you will be returned an object of type <strong class="classname">SDO_DAS_XML_Document</strong>, on which you can call the <span class="function"><strong>getRootDataObject()</strong></span> method to get the SDO data object which is the root of the SDO data graph. You can then use SDO operations to change the graph. In this example we alter the <var class="varname"><var class="varname">date</var></var>, <var class="varname"><var class="varname">firstName</var></var>, and <var class="varname"><var class="varname">lastName</var></var> properties. Then we use the <span class="function"><strong>saveFile()</strong></span> method to write the changed document back to the file system. The saveFile method has an optional extra integer argument which if specified will cause the XML DAS to format the XML, using the integer as the amount to indent by at each change in level on the document. </p></div> <div class="example-contents"><p> This will write the following to <var class="filename">letter-out.xml</var>. </p></div> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode"><?xml version="1.0" encoding="UTF-8"?> <FormLetter xmlns="http://letterSchema" xsi:type="FormLetter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <date>September 03, 2004</date> Mutual of Omaha Wild Kingdom, USA Dear <firstName>Anantoju</firstName> <lastName>Madhu</lastName> Please buy more shark repellent. Your premium is past due. </FormLetter></pre> </div> </div> </div> <div class="example" id="example-6698"> <p><strong>Example #2 Creating a new XML document</strong></p> <div class="example-contents"><p> The previous example loaded the document from a file. This example shows how to create an SDO data graph in memory. In this example it is then saved to an XML string. Furthermore, because the letter contains both structured and unstructured content, it uses <a href="sdo.sample.sequence.html" class="link">the Sequence API</a> as well assignments to properties to construct the data graph. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**<br /> * Create an XML document from scratch<br /> */<br /></span><span style="color: #007700">try {<br /> </span><span style="color: #0000BB">$xmldas </span><span style="color: #007700">= </span><span style="color: #0000BB">SDO_DAS_XML</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">"letter.xsd"</span><span style="color: #007700">);<br /> try {<br /> </span><span style="color: #0000BB">$doc </span><span style="color: #007700">= </span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">createDocument</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">$rdo </span><span style="color: #007700">= </span><span style="color: #0000BB">$doc</span><span style="color: #007700">-></span><span style="color: #0000BB">getRootDataObject</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">$seq </span><span style="color: #007700">= </span><span style="color: #0000BB">$rdo</span><span style="color: #007700">-></span><span style="color: #0000BB">getSequence</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"April 09, 2005"</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #DD0000">'date'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"Acme Inc. "</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"United Kingdom. "</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"Dear"</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"Tarun"</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #DD0000">"firstName"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"Nayaraaa"</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #DD0000">"lastName"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$rdo</span><span style="color: #007700">-></span><span style="color: #0000BB">lastName </span><span style="color: #007700">= </span><span style="color: #DD0000">"Nayar"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"Please note that your order number "</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">12345</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">" has been dispatched today. Thanks for your business with us."</span><span style="color: #007700">);<br /> print(</span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">saveString</span><span style="color: #007700">(</span><span style="color: #0000BB">$doc</span><span style="color: #007700">));<br /> } catch (</span><span style="color: #0000BB">SDO_Exception $e</span><span style="color: #007700">) {<br /> print(</span><span style="color: #0000BB">$e</span><span style="color: #007700">);<br /> }<br />} catch (</span><span style="color: #0000BB">SDO_Exception $e</span><span style="color: #007700">) {<br /> print(</span><span style="color: #DD0000">"Problem creating an XML document: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> The <span class="function"><strong>createDocument()</strong></span> method on the XML DAS returns a document object with a single root data object corresponding to an empty document element. The element name of the document element is known from the schema file. If there is any ambiguity about what the document element is, as there can be when more than one schema has been loaded into the same XML DAS, the element name and the namespace URI can be passed to the <span class="function"><strong>createDocument()</strong></span> method. </p> <p class="para"> This will emit the following output (line breaks have been inserted for readability): </p> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode"><?xml version="1.0" encoding="UTF-8"?> <FormLetter xmlns="http://letterSchema" xsi:type="FormLetter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <date>April 09, 2005</date> Acme Inc. United Kingdom. Dear <firstName>Tarun</firstName> <lastName>Nayar</lastName> Please note that your order number 12345 has been dispatched today. Thanks for your business with us. </FormLetter></pre> </div> </div> <div class="example" id="example-6699"> <p><strong>Example #3 Setting XML document properties</strong></p> <div class="example-contents"><p> This third example shows you how to set the XML version and encoding on the document object. These will be used when the XML is written out. If no XML declaration is wanted at all (perhaps you want to generate the XML as a string to embed in something) then you can use the <span class="function"><strong>setXMLDeclaration()</strong></span> method to suppress it. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**<br /> * Illustrate the calls that control the XML declaration<br /> */<br /> </span><span style="color: #0000BB">$xmldas </span><span style="color: #007700">= </span><span style="color: #0000BB">SDO_DAS_XML</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">"letter.xsd"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$document </span><span style="color: #007700">= </span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">loadFile</span><span style="color: #007700">(</span><span style="color: #DD0000">"letter.xml"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$document</span><span style="color: #007700">-></span><span style="color: #0000BB">setXMLVersion</span><span style="color: #007700">(</span><span style="color: #DD0000">"1.1"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$document</span><span style="color: #007700">-></span><span style="color: #0000BB">setEncoding</span><span style="color: #007700">(</span><span style="color: #DD0000">"ISO-8859-1"</span><span style="color: #007700">);<br /> print(</span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">saveString</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p> The XML version and encoding are set in the XML declaration at the top of the XML document. </p></div> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode"><?xml version="1.1" encoding="utf-8"?> .../...</pre> </div> </div> </div> <div class="example" id="example-6700"> <p><strong>Example #4 Using an open type</strong></p> <div class="example-contents"><p> This fourth example illustrates the use of an SDO open type and the use of the <span class="function"><strong>createDataObject()</strong></span> method. For this example we use the following two schema: </p></div> <div class="example-contents"><p> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode"><schema xmlns="http://www.w3.org/2001/XMLSchema"> <element name="jungle"> <complexType> <sequence> <any minOccurs="0" maxOccurs="unbounded"/> </sequence> </complexType> </element> </schema></pre> </div> </div> </p></div> <div class="example-contents"><p> Note the presence of the <var class="varname"><var class="varname">any</var></var> element in the definition. This first schema defines the <var class="varname"><var class="varname">jungle</var></var> complex type as containing a sequence of any other type. The other types that the example will use are defined in a second schema file: </p></div> <div class="example-contents"><p> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode"><schema xmlns= "http://www.w3.org/2001/XMLSchema"> <complexType name="snakeType"> <sequence> <element name= "name" type="string"/> <element name= "length" type="positiveInteger" /> </sequence> </complexType> <complexType name="bearType"> <sequence> <element name= "name" type="string"/> <element name= "weight" type="positiveInteger" /> </sequence> </complexType> <complexType name="pantherType"> <sequence> <element name= "name" type="string"/> <element name= "colour" type="string" /> </sequence> </complexType> </schema></pre> </div> </div> </p></div> <div class="example-contents"><p> Here is the example PHP code that uses these two schema files: </p></div> <div class="example-contents"><p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">/**<br /> * Illustrate open types and the use of the addTypes() method<br /> */<br /><br /></span><span style="color: #0000BB">$xmldas </span><span style="color: #007700">= </span><span style="color: #0000BB">SDO_DAS_XML</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">addTypes</span><span style="color: #007700">(</span><span style="color: #DD0000">"jungle.xsd"</span><span style="color: #007700">); </span><span style="color: #FF8000">// this is an open type i.e. the xsd specifies it can contain "any" type<br /></span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">addTypes</span><span style="color: #007700">(</span><span style="color: #DD0000">'animalTypes.xsd'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$baloo </span><span style="color: #007700">= </span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">createDataObject</span><span style="color: #007700">(</span><span style="color: #DD0000">''</span><span style="color: #007700">,</span><span style="color: #DD0000">'bearType'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$baloo</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">"Baloo"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$baloo</span><span style="color: #007700">-></span><span style="color: #0000BB">weight </span><span style="color: #007700">= </span><span style="color: #0000BB">800</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$bagheera </span><span style="color: #007700">= </span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">createDataObject</span><span style="color: #007700">(</span><span style="color: #DD0000">''</span><span style="color: #007700">,</span><span style="color: #DD0000">'pantherType'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$bagheera</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">"Bagheera"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bagheera</span><span style="color: #007700">-></span><span style="color: #0000BB">colour </span><span style="color: #007700">= </span><span style="color: #DD0000">'inky black'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$kaa </span><span style="color: #007700">= </span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">createDataObject</span><span style="color: #007700">(</span><span style="color: #DD0000">''</span><span style="color: #007700">,</span><span style="color: #DD0000">'snakeType'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$kaa</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">"Kaa"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$kaa</span><span style="color: #007700">-></span><span style="color: #0000BB">length </span><span style="color: #007700">= </span><span style="color: #0000BB">25</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$document </span><span style="color: #007700">= </span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">createDocument</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$do </span><span style="color: #007700">= </span><span style="color: #0000BB">$document</span><span style="color: #007700">-></span><span style="color: #0000BB">getRootDataObject</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$do</span><span style="color: #007700">-></span><span style="color: #0000BB">bear </span><span style="color: #007700">= </span><span style="color: #0000BB">$baloo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$do</span><span style="color: #007700">-></span><span style="color: #0000BB">panther </span><span style="color: #007700">= </span><span style="color: #0000BB">$bagheera</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$do</span><span style="color: #007700">-></span><span style="color: #0000BB">snake </span><span style="color: #007700">= </span><span style="color: #0000BB">$kaa</span><span style="color: #007700">;<br /><br />print(</span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">saveString</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">,</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </p></div> <div class="example-contents"><p> These two schema files are loaded into the XML DAS with first the <span class="function"><strong>create()</strong></span> and <span class="function"><strong>addTypes()</strong></span> methods. The <span class="function"><strong>createDataObject()</strong></span> method is used to create three separate data objects. In each case the namespaceURI and typename of the type are passed to the <span class="function"><strong>createDataObject()</strong></span> method: in this example the namespace URI is blank because no namespace is used in the schema. Once the three data objects - representing a bear, a panther and a snake - have been created, a document object is created with the <span class="function"><strong>createDocument()</strong></span> method. In this case there is no ambiguity about what the document element of the document should be - as the second schema file only defines complex types, the document element can only be the global <var class="varname"><var class="varname">jungle</var></var> element defined in the first schema. This document will have a single root data object corresponding to an empty document element <var class="varname"><var class="varname">jungle</var></var>. As this is an open type, properties can be added at will. When the first assignment is made to <var class="varname"><var class="varname">$do->bear</var></var>, a property <var class="varname"><var class="varname">bear</var></var> is added to the root data object: likewise for the next two assignments. When the document is written out by the <span class="function"><strong>saveString()</strong></span> method, the resulting document is: </p></div> <div class="example-contents"><p> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode"><?xml version="1.0" encoding="UTF-8"?> <jungle xsi:type="jungle" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <bear xsi:type="bearType"> <name>Baloo</name> <weight>800</weight> </bear> <panther xsi:type="pantherType"> <name>Bagheera</name> <colour>inky black</colour> </panther> <snake xsi:type="snakeType"> <name>Kaa</name> <length>25</length> </snake> </jungle></pre> </div> </div> </p></div> </div> <div class="example" id="example-6701"> <p><strong>Example #5 Finding out what you can from the document</strong></p> <div class="example-contents"><p> This example is intended to illustrate how you can find the element name and namespace of the document element from the XML Document object, and the SDO type and namespace from the root data object of the XML data object, and how they relate to one another. This can be difficult to understand because there are four method calls: two can be made against the Document object, and two that can be made against any data object including the root data object. Because of the rules that define how the SDO model is derived from the XML model, when the data object concerned is the root object that represents the document object for the document, only three possible values can come back from these four method calls. </p></div> <div class="example-contents"><p> The two method calls that can be made against the document are <span class="function"><strong>getRootElementName()</strong></span> and <span class="function"><strong>getRootEelementURI()</strong></span>. These return the element name and namespace of the document element, respectively. </p></div> <div class="example-contents"><p> The two method calls that can be made against any data object are <span class="function"><strong>getTypeName()</strong></span> and <span class="function"><strong>getTypeNamespaceURI()</strong></span>. These return the SDO type name and type namespace of the data object, respectively. </p></div> <div class="example-contents"><p> Always, calling <span class="function"><strong>getRootElementURI()</strong></span> on the document object will return the same value as calling <span class="function"><strong>getNamespaceURI()</strong></span> on the root data object. Essentially, the information is all derived from the first few lines of the schema file, where there are three distinct pieces of information. For illustration, here again are the first few lines of the letter.xsd that we used above. </p></div> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode"><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:letter="http://letterSchema" targetNamespace="http://letterSchema"> <xsd:element name="letters" type="letter:FormLetter"/> <xsd:complexType name="FormLetter" mixed="true"> .../...</pre> </div> </div> <div class="example-contents"><p> The three important values are: <ul class="itemizedlist"> <li class="listitem"> <p class="para"> <var class="varname"><var class="varname">letters</var></var>, the name of the document element </p> </li> <li class="listitem"> <p class="para"> <var class="varname"><var class="varname">FormLetter</var></var>, the name of the complex type of the document element. This is also the name of the SDO type of the root data object. </p> </li> <li class="listitem"> <p class="para"> <var class="varname"><var class="varname">http://letterSchema</var></var>, the namespace to which the document element belongs. This is also the namespaceURI of the SDO type of the root data object. </p> </li> </ul> It is part of the XML-SDO mapping rules that when the SDO model is built from the schema file, the typename and namespaceURI of the SDO types for the root element are taken from those of the complex type of the document element, where it exists. Hence in this example the typename of the root data object is FormLetter. In the event that there is no separate complex type definition for the document element, when the type is defined inline and is anonymous, the SDO type name will be the same as the element name. </p></div> <div class="example-contents"><p> The following program loads the letter document and checks the return values from each of the four calls. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**<br /> * Finding out what you can about the document and document element<br /> * This can be quite hard to understand because there are four calls<br /> * Two calls are made against the document<br /> * Two calls are made against the root data object and its model<br /> * Because of the SDO-XML mapping rules and how the SDO model is derived<br /> * from the XML model, only three possible values can come back from these four calls.<br /> * Always, $document->getRootElementURI() == (type of root data object)->namespaceURI <br /> * Essentially, it all comes form the first few lines of the xsd:<br /> * <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"<br /> * xmlns:letter="http://letterSchema"<br /> * targetNamespace="http://letterSchema"><br /> * <xsd:element name="letters" type="letter:FormLetter"/><br /> */<br /><br /></span><span style="color: #0000BB">$xmldas </span><span style="color: #007700">= </span><span style="color: #0000BB">SDO_DAS_XML</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">"letter.xsd"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$document </span><span style="color: #007700">= </span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">loadFile</span><span style="color: #007700">(</span><span style="color: #DD0000">"letter.xml"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$root_do </span><span style="color: #007700">= </span><span style="color: #0000BB">$document</span><span style="color: #007700">-></span><span style="color: #0000BB">getRootDataObject</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/**<br /> * The "root element name" is the element name of the document element<br /> * in this case 'letters'<br /> * This matches the 'name' attribute of the document element in the xsd and matches<br /> * the element name from the xml<br /> */<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"The document element name is " </span><span style="color: #007700">. </span><span style="color: #0000BB">$document</span><span style="color: #007700">-></span><span style="color: #0000BB">getRootElementName</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">-></span><span style="color: #0000BB">getRootElementName</span><span style="color: #007700">() == </span><span style="color: #DD0000">'letters'</span><span style="color: #007700">); </span><span style="color: #FF8000">// a property of the document<br /><br />/**<br /> * The "root element URI" is the namespace part of the element name of the document element<br /> * in this case 'http://letterSchema' since 'letters' is in that namespace<br /> * This is taken from the xsd and matches the namespace picked up from the xml<br /> */<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"The document element is in the namespace " </span><span style="color: #007700">. </span><span style="color: #0000BB">$document</span><span style="color: #007700">-></span><span style="color: #0000BB">getRootElementURI</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">-></span><span style="color: #0000BB">getRootElementURI</span><span style="color: #007700">() == </span><span style="color: #DD0000">'http://letterSchema'</span><span style="color: #007700">); </span><span style="color: #FF8000">// a property of the document<br /><br />/**<br /> * The type name is taken from the SDO model<br /> * The XML-SDO mapping rules make this either:<br /> * The name of the complexType if there is one (in this case there is)<br /> * The document element name if there no complexType<br /> * This is taken from the xsd <br /> */<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"The type name of the root data object is " </span><span style="color: #007700">. </span><span style="color: #0000BB">$root_do</span><span style="color: #007700">-></span><span style="color: #0000BB">getTypeName</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">$root_do</span><span style="color: #007700">-></span><span style="color: #0000BB">getTypeName</span><span style="color: #007700">() == </span><span style="color: #DD0000">'FormLetter'</span><span style="color: #007700">); <br /><br /></span><span style="color: #FF8000">/**<br /> * The type's namespaceURI is taken from the SDO model<br /> * The XML-SDO mapping rules ensure that this will always be the same as <br /> * the namepace URI of the document element<br /> */<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"The namespaceURI of the root data object is " </span><span style="color: #007700">. </span><span style="color: #0000BB">$root_do</span><span style="color: #007700">-></span><span style="color: #0000BB">getTypeNamespaceURI</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">$root_do</span><span style="color: #007700">-></span><span style="color: #0000BB">getTypeNamespaceURI</span><span style="color: #007700">() == </span><span style="color: #DD0000">'http://letterSchema'</span><span style="color: #007700">); <br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p> The output from this program is as follows: </p></div> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode">The document element name is letters The document element is in the namespace http://letterSchema The type name of the root data object is FormLetter The namespaceURI of the root data object is http://letterSchema</pre> </div> </div> </div> <div class="example" id="example-6702"> <p><strong>Example #6 Printing the SDO model</strong></p> <div class="example-contents"><p> The XML DAS provides a simple means to see what types and properties have been loaded. The php "print" or "echo" instruction will print out the types and properties. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**<br /> * Illustrate printing out the model<br /> */<br /><br /></span><span style="color: #0000BB">$xmldas </span><span style="color: #007700">= </span><span style="color: #0000BB">SDO_DAS_XML</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">"letter.xsd"</span><span style="color: #007700">);<br />print </span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p> The output from this program is as follows: </p></div> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode">object(SDO_XML_DAS)#1 { 18 types have been defined. The types and their properties are:: 1. commonj.sdo:BigDecimal 2. commonj.sdo:BigInteger 3. commonj.sdo:Boolean 4. commonj.sdo:Byte 5. commonj.sdo:Bytes 6. commonj.sdo:ChangeSummary 7. commonj.sdo:Character 8. commonj.sdo:DataObject 9. commonj.sdo:Date 10. commonj.sdo:Double 11. commonj.sdo:Float 12. commonj.sdo:Integer 13. commonj.sdo:Long 14. commonj.sdo:Short 15. commonj.sdo:String 16. commonj.sdo:URI 17. http://letterSchema:FormLetter - date (commonj.sdo:String) - firstName (commonj.sdo:String) - lastName (commonj.sdo:String) 18. http://letterSchema:RootType - letters (http://letterSchema:FormLetter)</pre> </div> </div> </div> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sdo-das-xml.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.sdo-das-xml.html">SDO DAS XML Functions</a></div> <div class="up"><a href="book.sdo-das-xml.html">SDO DAS XML</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>