<?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</title>
	<atom:link href="http://www.dimuthu.org/tag/xml/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>Using AXIOM/C As The XML Object Model</title>
		<link>http://www.dimuthu.org/blog/2008/12/03/using-axiomc-as-the-xml-object-model/</link>
		<comments>http://www.dimuthu.org/blog/2008/12/03/using-axiomc-as-the-xml-object-model/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 16:35:04 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[axiom]]></category>
		<category><![CDATA[axis2/c]]></category>
		<category><![CDATA[DataServices]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[AXIOM/C]]></category>
		<category><![CDATA[axiom_node_t*]]></category>
		<category><![CDATA[Buffer]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=754</guid>
		<description><![CDATA[In Apache Axis2/C AXIOM is used as the basic object model to represent XML. AXIOM provide a DOM like API that allows to traverse and build the XML very easily. Anyway in underneath, AXIOM is different from DOM, as it has used some techniques to optimize the parsing of the XML as suited specially for [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://ws.apache.org/axis2/c/">Apache Axis2/C</a> AXIOM is used as the basic object model to represent XML. AXIOM provide a DOM like API that allows to traverse and build the XML very easily.</p>
<p>Anyway in underneath, AXIOM is different from DOM, as it has used some techniques to optimize the parsing of the XML as suited specially for SOAP message processing in web services. For an example the SOAP processor can validate a SOAP message by reading only some parts of the SOAP header fields, and if it is not valid, they can completely skip processing the body part. And since AXIOM is designed to built from a stream of data retrieved from a transport, sometimes SOAP processors can validate the message without the need of reading the full stream.</p>
<p>Anyway there should be lot of application that needs this optimization in parsing XMLs. They can easily adapt AXIOM/C to their application. Here is an <a href="http://ws.apache.org/axis2/c/docs/om_tutorial.html">AXIOM/C tutorial</a> that covers both parsing and building XMLs from AXIOM. In this post I&#8217;d like to mention a code that can be used to retrieve an AXIOM from a String (char buffer) which we call as deserialization.</p>
<pre class="c">    axiom_node_t* AXIS2_CALL
    deserialize_my_buffer <span style="color: #66cc66;">(</span>
        <span style="color: #993333;">const</span> axutil_env_t * env,
        <span style="color: #993333;">char</span> *buffer<span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        axiom_xml_reader_t *reader = <span style="font-weight: bold; color: #000000;">NULL</span>;
        axiom_stax_builder_t *builder = <span style="font-weight: bold; color: #000000;">NULL</span>;
        axiom_document_t *document = <span style="font-weight: bold; color: #000000;">NULL</span>;
        axiom_node_t *payload = <span style="font-weight: bold; color: #000000;">NULL</span>;

        reader = axiom_xml_reader_create_for_memory <span style="color: #66cc66;">(</span>env,
            buffer, axutil_strlen <span style="color: #66cc66;">(</span>buffer<span style="color: #66cc66;">)</span>,
            AXIS2_UTF_8, AXIS2_XML_PARSER_TYPE_BUFFER<span style="color: #66cc66;">)</span>;

        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!reader<span style="color: #66cc66;">)</span>
        <span style="color: #66cc66;">{</span>
            <span style="color: #b1b100;">return</span> <span style="font-weight: bold; color: #000000;">NULL</span>;
        <span style="color: #66cc66;">}</span>

        builder = axiom_stax_builder_create <span style="color: #66cc66;">(</span>env, reader<span style="color: #66cc66;">)</span>;

        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!builder<span style="color: #66cc66;">)</span>
        <span style="color: #66cc66;">{</span>
            <span style="color: #b1b100;">return</span> <span style="font-weight: bold; color: #000000;">NULL</span>;
        <span style="color: #66cc66;">}</span>
        document = axiom_stax_builder_get_document <span style="color: #66cc66;">(</span>builder, env<span style="color: #66cc66;">)</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!document<span style="color: #66cc66;">)</span>
        <span style="color: #66cc66;">{</span>
            AXIS2_LOG_ERROR <span style="color: #66cc66;">(</span>env-&gt;log, AXIS2_LOG_SI,
                    <span style="color: #ff0000;">"Document is null for deserialization"</span><span style="color: #66cc66;">)</span>;
            <span style="color: #b1b100;">return</span> <span style="font-weight: bold; color: #000000;">NULL</span>;
        <span style="color: #66cc66;">}</span>

        payload = axiom_document_get_root_element <span style="color: #66cc66;">(</span>document, env<span style="color: #66cc66;">)</span>;

        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!payload<span style="color: #66cc66;">)</span>
        <span style="color: #66cc66;">{</span>
            AXIS2_LOG_ERROR <span style="color: #66cc66;">(</span>env-&gt;log, AXIS2_LOG_SI,
                    <span style="color: #ff0000;">"Root element of the document is not found"</span><span style="color: #66cc66;">)</span>;
            <span style="color: #b1b100;">return</span> <span style="font-weight: bold; color: #000000;">NULL</span>;
        <span style="color: #66cc66;">}</span>
        axiom_document_build_all <span style="color: #66cc66;">(</span>document, env<span style="color: #66cc66;">)</span>;

        axiom_stax_builder_free_self <span style="color: #66cc66;">(</span>builder, env<span style="color: #66cc66;">)</span>;

        <span style="color: #b1b100;">return</span> payload;
    <span style="color: #66cc66;">}</span></pre>
<p>Regardless of the fact this piece of code is been used many time by Axis2 and application that uses Axis2, it has never been identified as a core AXIOM function. I think it is better we have this function as an alternative method to create an axiom.</p>
<pre class="c">axiom_node_t *AXIS2_CALL
axiom_node_create_from_buffer<span style="color: #66cc66;">(</span><span style="color: #993333;">const</span> axutil_env_t *env, axis2_char_t *buffer<span style="color: #66cc66;">)</span>;</pre>
<p>I already suggested this in Axis2/C mailing list and hopefully it will be included from the next release.</p>
<p>Here when we create the axiom tree function from the character buffer, we used &#8220;axiom_xml_reader_create_for_memory&#8221; function. Anyway whenever transport read data stream from wire it always uses the &#8220;axiom_xml_reader_create_for_io&#8221; function.</p>
<pre class="c">    <span style="font-style: italic; color: #808080;">/**
     * This create an instance of axiom_xml_reader to
     * parse a xml document in a buffer. It takes a callback
     * function that takes a buffer and the size of the buffer
     * The user must implement a function that takes in buffer
     * and size and fill the buffer with specified size
     * with xml stream, parser will call this function to fill the
     * buffer on the fly while parsing.
     * @param env environment MUST NOT be NULL.
     * @param read_input_callback() callback function that fills
     * a char buffer.
     * @param close_input_callback() callback function that closes
     * the input stream.
     * @param ctx, context can be any data that needs to be passed
     * to the callback method.
     * @param encoding encoding scheme of the xml stream
     */</span>
    AXIS2_EXTERN axiom_xml_reader_t *AXIS2_CALL
    axiom_xml_reader_create_for_io<span style="color: #66cc66;">(</span>
        <span style="color: #993333;">const</span> axutil_env_t * env,
        AXIS2_READ_INPUT_CALLBACK read_callback,
        AXIS2_CLOSE_INPUT_CALLBACK close_callback,
        <span style="color: #993333;">void</span> *ctx,
        <span style="color: #993333;">const</span> axis2_char_t * encoding<span style="color: #66cc66;">)</span>;</pre>
<p>As you may have noticed it requires us to implement a &#8220;read_callback&#8221; function. Here is an example function prototype to implement this callback.</p>
<pre class="c">    <span style="color: #993333;">int</span> AXIS2_CALL
    some_function<span style="color: #66cc66;">(</span>
            <span style="color: #993333;">char</span> *buffer,
            <span style="color: #993333;">int</span> size,
            <span style="color: #993333;">void</span> *ctx<span style="color: #66cc66;">)</span>;</pre>
<p>This function will be called by the parser as required to parse the XML read from some stream.</p>
<p>So if your application involves reading data from a stream you are always recommended to use this function (i.e. &#8220;axiom_xml_reader_create_for_io&#8221;) instead of &#8220;axiom_xml_read_create_for_buffer&#8221; to create the AXIOM model more effectively.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/12/03/using-axiomc-as-the-xml-object-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XPath in SimpleXML</title>
		<link>http://www.dimuthu.org/blog/2008/09/30/xpath-in-simplexml/</link>
		<comments>http://www.dimuthu.org/blog/2008/09/30/xpath-in-simplexml/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 17:40:03 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xpath]]></category>
		<category><![CDATA[simplexml]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=416</guid>
		<description><![CDATA[SimpleXML as it name imply, is a very simple API to traverse XML implemented specially in PHP language. It is very similar to the XPath, but since it has more PHP friendly syntax PHP developers really like to use it. As an Example for this XML: &#60;dwml&#62; &#60;data&#62; &#60;location&#62; &#60;location-key&#62;point1&#60;/location-key&#62; &#60;point latitude="37.39" longitude="-122.07"&#62;&#60;/point&#62; &#60;/location&#62; &#60;/data&#62; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://php.net/simplexml">SimpleXML</a> as it name imply, is a very simple API to traverse XML implemented specially in PHP language. It is very similar to the XPath, but since it has more PHP friendly syntax PHP developers really like to use it.<br />
As an Example for this XML:</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dwml<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;data<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;location<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;location-key<span style="font-weight: bold; color: black;">&gt;</span></span></span>point1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/location-key<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;point</span> <span style="color: #000066;">latitude</span>=<span style="color: #ff0000;">"37.39"</span> <span style="color: #000066;">longitude</span>=<span style="color: #ff0000;">"-122.07"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/point<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/location<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/data<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  .....
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dwml<span style="font-weight: bold; color: black;">&gt;
</span></span></span></pre>
<p>XPATH Query to take the latitude in more general way</p>
<pre><em>/dwml/data/location/point/@latitude
</em></pre>
<p>Where as with simple XML it is just a familiar PHP statement,</p>
<pre><em>$simplexml-&gt;data-&gt;location-&gt;point-&gt;attributes()-&gt;latitude</em></pre>
<p>Anyway still you can use the xpath inside your simplexml code. You can execute xpath queries by calling xpath function from any SimpleXMLEelment. It will return an array of SimpleXMLElement that match your query. So for the above example your XPath query would be something  like this,</p>
<pre class="php"><span style="color: #0000ff;">$simplexml</span>= <span style="font-weight: bold; color: #000000;">new</span> SimpleXMLElement<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$xml</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$lats</span> =  <span style="color: #0000ff;">$simplexml</span>-&gt;<span style="color: #006600;">xpath</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'/dwml/data/location/point/@latitude'</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$lats</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">]</span>;</pre>
<p>This simplicity allows you to choose between these two methods interchangeably as best fit per your application. Here are some cases that I think use of XPath is more easy.</p>
<p><strong>Ability to use of XPath shorthand</strong><br />
Take the above example XML it self. If there is only one attribute named &#8216;latitude&#8217; throughout the XML you can call that value by</p>
<pre>//@latitude</pre>
<p><strong>If XML node name or attribute name contains characters like &#8216;-&#8217; which are not allowed in PHP for variable names</strong><br />
In the example if you want to access the value inside &#8216;location-key&#8217; node using simplexml it would be like,</p>
<pre><a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$simplexml</span>-&gt;<span style="color: #006600;">data</span>-&gt;<span style="color: #006600;">location</span>-&gt;<span style="color: #006600;">location-key</span>;</pre>
<p>This will not give you the expected result as PHP will try to think &#8216;location&#8217; and &#8216;key&#8217; as two taken in &#8216;location-key&#8217;. So this particular code can be replaced with the xpath function.</p>
<pre class="php"><span style="color: #0000ff;">$keys</span> =  <span style="color: #0000ff;">$simplexml</span>-&gt;<span style="color: #006600;">xpath</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'/dwml/data/location/location-key'</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$keys</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">]</span>;</pre>
<p><strong>You want to iterate through node with a same name in an XML</strong></p>
<p>If the nodes which we want to iterate is in organized positions  in an XML (like the one in following) both approaches can be used with same easiness.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;root<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>value1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>value2<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>value3<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>value4<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>value5<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/root<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>But how if the &#8216;mynode&#8217; was in different locations in an XML like this,</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;root<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;anothernode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>value1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/anothernode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;anotheranothernode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;anotheranotheranothernode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>value2<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/anotheranotheranothernode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>value3<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/anotheranothernode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;<strong>mynode</strong><span style="font-weight: bold; color: black;">&gt;</span></span></span>value4<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/mynode<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/root<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>You can iterate all the &#8216;mynode&#8217; nodes with the following xpath query.</p>
<pre>//mynode</pre>
<p>Note that this case can be handled easily in DOM with the getElementsByName.</p>
<p><strong>To use the power of XPath functions and Axes</strong><br />
You can use the XPath functions like last(), position() and even string manipulation functions like substring() in a XPath statement.<br />
For an example in the above example, if you want only take the value of last &#8216;mynode&#8217; just use this expression</p>
<pre>//mynode[last()]</pre>
<p>And you can use the power of axes in Xpath Queries. If you want to iterate all the ancestors from current node just use this query</p>
<pre>'ancestor::*'</pre>
<p><strong>Access elements with different namespaces</strong></p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;saleItems<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ns1:car</span> xmlns:ns1=<span style="color: #ff0000;">"http:/toyota.xxx.com"</span><span style="font-weight: bold; color: black;">&gt;</span></span>$3000<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:car<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ns2:car</span> xmlns:ns2=<span style="color: #ff0000;">"http:/suziki.rrr.com"</span><span style="font-weight: bold; color: black;">&gt;</span></span>$4000<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns2:car<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/saleItems<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>You want to extract the cars from simpleXML. You can do this by following code.</p>
<pre class="php"><span style="color: #0000ff;">$simplexml</span>= <span style="font-weight: bold; color: #000000;">new</span> SimpleXMLElement<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$xml</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$ns1_childs</span> = <span style="color: #0000ff;">$simplexml</span>-&gt;<span style="color: #006600;">children</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"http:/toyota.xxx.com"</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$ns1_childs</span>-&gt;<span style="color: #006600;">car</span>;

<span style="color: #0000ff;">$ns2_childs</span> = <span style="color: #0000ff;">$simplexml</span>-&gt;<span style="color: #006600;">children</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"http:/suziki.rrr.com"</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$ns2_childs</span>-&gt;<span style="color: #006600;">car</span>;</pre>
<p>Every time you access a different namespace you have to call the children method with the namespace as an argument.</p>
<p>If you use XPath approach, you first register the namespces with an prefix and just use those prefix in your XPath queries.</p>
<pre class="php"><span style="color: #0000ff;">$simplexml</span>= <span style="font-weight: bold; color: #000000;">new</span> SimpleXMLElement<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$xml</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$simplexml</span>-&gt;<span style="color: #006600;">registerXPathNamespace</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"p1"</span>, <span style="color: #ff0000;">"http:/toyota.xxx.com"</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$simplexml</span>-&gt;<span style="color: #006600;">registerXPathNamespace</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"p2"</span>, <span style="color: #ff0000;">"http:/suziki.rrr.com"</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$toyota_cars</span> = <span style="color: #0000ff;">$simplexml</span>-&gt;<span style="color: #006600;">xpath</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'//p1:car'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$suziki_cars</span> = <span style="color: #0000ff;">$simplexml</span>-&gt;<span style="color: #006600;">xpath</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'//p2:car'</span><span style="color: #66cc66;">)</span>;

<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$toyota_cars</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">]</span>;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$suziki_cars</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">]</span>;</pre>
<p>SimpleXML is simple and powerful in its native form. But whenever it is impossible or difficult to use you don&#8217;t need to go back for tedious DOM or manual string manipulation. You can use the xpath queries to get the work done within the simplexml environment itself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/09/30/xpath-in-simplexml/feed/</wfw:commentRss>
		<slash:comments>20</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>
		<item>
		<title>Perl XML Parsers And My Story</title>
		<link>http://www.dimuthu.org/blog/2008/07/28/perl-xml-parsers-and-my-story/</link>
		<comments>http://www.dimuthu.org/blog/2008/07/28/perl-xml-parsers-and-my-story/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 16:50:27 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[personal]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/perl]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[libxml]]></category>
		<category><![CDATA[namespace]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[twig]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xmlxpath]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=61</guid>
		<description><![CDATA[Last week I had an opportunity to write some CGI scripts in Perl. It is like going few years back in web development. And it gave me the answer why PHP become favorite over Perl among the web developers. It is not just PHP&#8217;s C-like friendly syntax, but also the ability to write inline script [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I had an opportunity to write some CGI scripts in Perl. It is like going few years back in web development. And it gave me the answer why PHP become favorite over Perl among the web developers. It is not just PHP&#8217;s C-like friendly syntax, but also the ability to write inline script in html may have been a big factor.</p>
<p>In there we came across parsing following type of XML.</p>
<pre><span style="font-weight: bold; color: black;">&lt;ns1:person</span>
       xmlns:ns1=<span style="color: #ff0000;">"http://dimuthu.org/example/perl_xml/xsd"</span><span style="font-weight: bold; color: black;">&gt;</span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ns1:name<span style="font-weight: bold; color: black;">&gt;</span></span></span>PQR XYZ<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ns1:age<span style="font-weight: bold; color: black;">&gt;</span></span></span>25<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:age<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:person<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>We started to use <a href="http://search.cpan.org/~mirod/XML-Twig-3.32/">XML::Twig</a>. And it is pretty straightforward. Here was our code.</p>
<pre><span style="font-weight: bold; color: #000000;">use</span> XML::<span style="color: #006600;">Twig</span>;

<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$xml_str</span> = &lt;&lt;E;
<span style="font-weight: bold; color: black;">&lt;ns1:person</span>
    xmlns:ns1=<span style="color: #ff0000;">"http://dimuthu.org/example/perl_xml/xsd"</span><span style="font-weight: bold; color: black;">&gt;</span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ns1:name<span style="font-weight: bold; color: black;">&gt;</span></span></span>PQR XYZ<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ns1:age<span style="font-weight: bold; color: black;">&gt;</span></span></span>25<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:age<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ns1:person<span style="font-weight: bold; color: black;">&gt;</span></span></span>
E

<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$xt</span> = XML::<span style="color: #006600;">Twig</span>-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$xt</span>-&gt;<span style="color: #006600;">parse</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$xml_str</span><span style="color: #66cc66;">)</span>;

<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$txt</span> = <span style="color: #0000ff;">$xt</span>-&gt;<span style="color: #006600;">root</span>-&gt;<span style="color: #006600;">findvalue</span><span style="color: #66cc66;">(</span>"//ns1:name"<span style="color: #66cc66;">)</span>;
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$txt</span>;</pre>
<p>OK. It was working.</p>
<p>Anyway in practice we found that this is not the only way we receive the xml. That is the namespace prefix can be different. When you write an XML to an XML schema you are free to have your own prefixes for the namespaces. And in fact in practice different people, programs and vendors uses different prefixes.</p>
<p>So our program should be able to parse following XML too. (see the namespace prefix is changed)</p>
<pre><span style="font-weight: bold; color: black;">&lt;ns0:person</span>
    xmlns:ns0=<span style="color: #ff0000;">"http://dimuthu.org/example/perl_xml/xsd"</span><span style="font-weight: bold; color: black;">&gt;</span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span></span><span style="font-weight: bold; color: black;">ns0</span><span style="color: #009900;"><span style="font-weight: bold; color: black;">:name<span style="font-weight: bold; color: black;">&gt;</span></span></span>PQR XYZ<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/</span></span><span style="font-weight: bold; color: black;">ns0</span><span style="color: #009900;"><span style="font-weight: bold; color: black;">:name<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="font-weight: bold; color: black;">ns0</span><span style="color: #009900;"><span style="font-weight: bold; color: black;">:age<span style="font-weight: bold; color: black;">&gt;</span></span></span>25<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/</span></span><span style="font-weight: bold; color: black;">ns0</span><span style="color: #009900;"><span style="font-weight: bold; color: black;">:age<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="font-weight: bold; color: black;">ns0</span><span style="color: #009900;"><span style="font-weight: bold; color: black;">:person<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>And with the default namespace.</p>
<pre><span style="font-weight: bold; color: black;">&lt;person</span> xmlns=<span style="color: #ff0000;">"http://dimuthu.org/example/perl_xml/xsd"</span><span style="font-weight: bold; color: black;">&gt;</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;">name<span style="font-weight: bold; color: black;">&gt;</span></span></span>PQR XYZ<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;">name<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;">age<span style="font-weight: bold; color: black;">&gt;</span></span></span>25<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;">age<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;">person<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Both these occasions our code failed. And normally whenever there is an API to parse XPath, you can register namespaces. But XML::Twit there was no something like that. That made us to jump to use <a href="http://search.cpan.org/~pajas/XML-LibXML-1.66/">XML::LibXML</a>. It is apparently little complicated than the simple Twig API. But it did work. (Look later for the code using XML::LibXML)</p>
<p>The story is actually there is a way to register namespaces in Twit, in fact it is not in  XML::Twit, it is in the XML::Twit:XPath module which is hardly any documented. It just duplicate the Twit API adding some functionalities to deal with namespaces. Apparently not the most elegant way of designing an API. Anyway Here is the code that works written using XML::Twit:XPath.</p>
<pre><span style="font-weight: bold; color: #000000;">use</span> XML::<span style="color: #006600;">Twig</span>;
<span style="font-weight: bold; color: #000000;">use</span> XML::<span style="color: #006600;">Twig</span>::<span style="color: #006600;">XPath</span>;

<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$xml_str</span> = &lt;&lt;E;

<span style="font-weight: bold; color: black;">&lt;person</span> xmlns=<span style="color: #ff0000;">"http://dimuthu.org/example/perl_xml/xsd"</span><span style="font-weight: bold; color: black;">&gt;</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;">name<span style="font-weight: bold; color: black;">&gt;</span></span></span>PQR XYZ<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;">name<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;">age<span style="font-weight: bold; color: black;">&gt;</span></span></span>25<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;">age<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;">person<span style="font-weight: bold; color: black;">&gt;</span></span></span>

E

<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$xtp</span> = XML::<span style="color: #006600;">Twig</span>::<span style="color: #006600;">XPath</span>-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$xtp</span>-&gt;<span style="color: #006600;">parse</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$xml_str</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$xtp</span>-&gt;<span style="color: #006600;">set_namespace</span><span style="color: #66cc66;">(</span>'ns',
       'http://dimuthu.org/example/perl_xml/xsd'<span style="color: #66cc66;">)</span>;

<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$txt</span> = <span style="color: #0000ff;">$xtp</span>-&gt;<span style="color: #006600;">root</span>-&gt;<span style="color: #006600;">findvalue</span><span style="color: #66cc66;">(</span>'//ns:name'<span style="color: #66cc66;">)</span>;
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$txt</span>;</pre>
<p>Note that I have register the namespace to the prefix &#8216;ns&#8217;, so in xpath quires I can use this prefix to refer namespaces.</p>
<p>Anyway Perl is not bad, it has dozens of modules to do the same thing. So just for the reference I will note down it here,</p>
<p>Using <a href="http://search.cpan.org/~msergeant/XML-XPath-1.13/XPath.pm">XML:XPath</a>,</p>
<pre><span style="font-weight: bold; color: #000000;">use</span> XML::<span style="color: #006600;">XPath</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$xml_str</span> = &lt;&lt;E;

<span style="font-weight: bold; color: black;">&lt;person</span> xmlns=<span style="color: #ff0000;">"http://dimuthu.org/example/perl_xml/xsd"</span><span style="font-weight: bold; color: black;">&gt;</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;">name<span style="font-weight: bold; color: black;">&gt;</span></span></span>PQR XYZ<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;">name<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;">age<span style="font-weight: bold; color: black;">&gt;</span></span></span>25<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;">age<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;">person<span style="font-weight: bold; color: black;">&gt;</span></span></span>

E

<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$xp</span> = XML::<span style="color: #006600;">XPath</span>-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">(</span>xml =&gt; <span style="color: #0000ff;">$xml_str</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$xp</span>-&gt;<span style="color: #006600;">set_namespace</span><span style="color: #66cc66;">(</span>'ns',
       'http://dimuthu.org/example/perl_xml/xsd<span style="color: #66cc66;">')</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$txt</span> = <span style="color: #0000ff;">$xp</span>-&gt;<span style="color: #006600;">findvalue</span><span style="color: #66cc66;">('</span>//ns:name'<span style="color: #66cc66;">)</span>; <span style="font-style: italic; color: #808080;"># get name </span>

<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$txt</span>;</pre>
<p>Then Using <a href="http://search.cpan.org/~pajas/XML-LibXML-1.66/LibXML.pod">XML::LibXML</a>;</p>
<pre><span style="font-weight: bold; color: #000000;">use</span> XML::<span style="color: #006600;">LibXML</span>;

<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$xml_str</span> = &lt;&lt;E;

<span style="font-weight: bold; color: black;">&lt;person</span> xmlns=<span style="color: #ff0000;">"http://dimuthu.org/example/perl_xml/xsd"</span><span style="font-weight: bold; color: black;">&gt;</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;">name<span style="font-weight: bold; color: black;">&gt;</span></span></span>PQR XYZ<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;">name<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;">age<span style="font-weight: bold; color: black;">&gt;</span></span></span>25<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;">age<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;">person<span style="font-weight: bold; color: black;">&gt;</span></span></span>

E

<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$xl</span>= XML::<span style="color: #006600;">LibXML</span>-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$xml</span> = <span style="color: #0000ff;">$xl</span>-&gt;<span style="color: #006600;">parse_string</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$xml_str</span><span style="color: #66cc66;">)</span>;

<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$xpc</span> = XML::<span style="color: #006600;">LibXML</span>::<span style="color: #006600;">XPathContext</span>-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$xml</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$xpc</span>-&gt;<span style="color: #006600;">registerNs</span><span style="color: #66cc66;">(</span>'ns',
        'http://dimuthu.org/example/perl_xml/xsd<span style="color: #66cc66;">')</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$txt</span> = <span style="color: #0000ff;">$xpc</span>-&gt;<span style="color: #006600;">findvalue</span><span style="color: #66cc66;">(</span>'//ns:name'<span style="color: #66cc66;">)</span>; <span style="font-style: italic; color: #808080;"># get name </span>

<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$txt</span>;</pre>
<p>So it is all for this post, And one reason I didn&#8217;t tell you earlier and so obvious why PHP is popular over Perl. Look at<a href="http://php.net/simplexml"> http://php.net/dom</a> or <a href="http://php.net/simplexml">http://php.net/simplexml.</a> It has a great documentation for every function to the every needle. Whenever Perl, Ruby thinking of going pass PHP, they have to consider this aspect too more seriously.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/07/28/perl-xml-parsers-and-my-story/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

