Search code examples
phpxmldocbook-5

Avoid cutting markup elements within xml nodes using PHP


I want to parse a DocBook XML file with PHP. SimpleXMLElement cuts off some markup elements.

<section>
<title>SUSPEND</title>
    <para>The <database>SUSPEND</database> command <quote>breaks</quote> the atomicity of the block in which it is located.</para>
    <para>...</para>
</section>

I use this code to parse my para elements:

$paras = $xml->xpath("//*[starts-with(local-name(), 'para')]");
foreach($paras as $para) {
   echo $para[0];
}

This cuts off my markup

<database>SUSPEND</database> and <quote>breaks</quote>

resulting in

The command the atomicity of the block in which it is located.

But what I need is:

The <database>SUSPEND</database> command <quote>breaks</quote> the atomicity of the block in which it is located.

So how can I get the whole node including its elements as string?


Solution

  • Just use asXML method to get tree with root at element found

    foreach($paras as $para) {
       echo $para->asXML();
    }