<?php $xml = <<<XML <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="http://www.accessdata.fda.gov/spl/stylesheet/spl.xsl" type="text/xsl"?> <document xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 http://www.accessdata.fda.gov/spl/schema/spl.xsd"> <component> <structuredBody> <component> <section ID="Lfba523bc-b21a-4a40-ae94-828271f8125f"> <id root="35e360f7-dc4d-5dd9-e054-00144ff88e88"/> <code code="51945-4" codeSystem="2.16.840.1.113883.6.1" displayName="PACKAGE LABEL.PRINCIPAL DISPLAY PANEL"/> <title>HERE IS TITLE 1</title> <text> <paragraph>This is Paragraph </paragraph> <paragraph>This is Paragraph 1</paragraph> <paragraph> <renderMultiMedia referencedObject="L30b39868-2c02-4f22-817f-3fc8ff059193"/> </paragraph> <paragraph>This is Paragraph 2</paragraph> <paragraph> <renderMultiMedia referencedObject="L7aa8dc60-ea4e-4c83-a8bd-ddf038c4b6bc"/> </paragraph> <paragraph>This is Paragraph 3</paragraph> <paragraph>This is Paragraph 4</paragraph> <paragraph> <renderMultiMedia referencedObject="L034834ef-4c2d-4d18-9fe0-7399530720ad"/> </paragraph> <paragraph/> </text> <effectiveTime value="20150909"/> <component> <observationMedia ID="L30b39868-2c02-4f22-817f-3fc8ff059193"> <text>image description</text> <value mediaType="image/jpeg" xsi:type="ED"> <reference value="Label1.jpg"/> </value> </observationMedia> </component> <component> <observationMedia ID="L7aa8dc60-ea4e-4c83-a8bd-ddf038c4b6bc"> <text>image description</text> <value mediaType="image/jpeg" xsi:type="ED"> <reference value="Label2.jpg"/> </value> </observationMedia> </component> <component> <observationMedia ID="L034834ef-4c2d-4d18-9fe0-7399530720ad"> <text>image description</text> <value mediaType="image/jpeg" xsi:type="ED"> <reference value="Label3.jpg"/> </value> </observationMedia> </component> </section> </component> </structuredBody> </component> </document> XML; // in DOM you load the XML into a document $document = new DOMDocument(); $document->loadXml($xml); // and create and Xpath object for it $xpath = new DOMxpath($document); // on this object you cen register aliases/prefixes for namespaces $xpath->registerNamespace('root', 'urn:hl7-org:v3'); // xpath expression with location paths return node lists that are iterable foreach ($xpath->evaluate('//root:section') as $component) { // but if you cast the result an Xpath expression can return a scalar value as well echo $xpath->evaluate('string(title)', $component), "\n"; // take note of the second argument, it defines the context for the xpath expression foreach($xpath->evaluate('.//root:paragraph', $component) as $paragraph) { // you can access attributes the same way, but attributes without a prefix are never in a namespace $referenceId = $xpath->evaluate('string(root:renderMultiMedia/@referencedObject)', $paragraph); if (empty($referenceId)) { // this will output the first text child node of the paragraph echo 'TEXT: ', $xpath->evaluate('string(text())', $paragraph), "\n"; } else { // expression that start with a / use the document context foreach ($xpath->evaluate("//root:observationMedia[@ID='{$referenceId}'][1]") as $reference) { echo 'REF:', $xpath->evaluate('string(root:value/root:reference/@value)', $reference), "\n"; } } } }
You have javascript disabled. You will not be able to edit any code.
Value for `_results` contains invalid data `array`