在PHP SimpleXML中替换引用对象值

时间:2017-10-02 07:18:49

标签: php xml simplexml

我正在尝试回显图像URL而不是referencedObject。我在正确的位置获取Referenced Object但是当我尝试将其替换为与referencedObject相对应的图像URL时,代码不会在正确的位置被替换,它显示在文本上方大约2行。 / p>

将SimpleXML与PHP结合使用。

<component>
 <section ID="Ld8e0a7b8-429c-4a7d-84ff-8b4698b7adc7">
 <title>DESCRIPTION </title>
 <text>
    <paragraph>Lorem ipsum dolor sit amet, consectetur adipiscing elit...</paragraph>

   <paragraph>Here is the image 1 </paragraph>
   <paragraph>
       <renderMultiMedia referencedObject="Lc0d31387-d262-4702-8053-0207e1a3fc91"/>
    </paragraph>
   <paragraph>Here is Image 2 , tempor incidid labore et dolore magna aliqua. Ut enim ad minim </paragraph>
    <paragraph>
      <renderMultiMedia referencedObject="L3dcd8ff8-678a-4e9f-8bb2-d6f3c35d1c10"/>
    </paragraph>
   <paragraph>tempor incidid labore et dolore magna aliqua. Ut enim ad minim </paragraph>
    <paragraph>tempor incidid labore et dolore magna aliqua. Ut enim ad minim </paragraph>
    <paragraph/>
</text>
<component>
    <observationMedia ID="Lc0d31387-d262-4702-8053-0207e1a3fc91">
       <text>image description</text>
       <value mediaType="image/jpeg" xsi:type="ED">
          <reference value="Image1.jpg"/>
       </value>
    </observationMedia>
 </component>
 <component>
  <observationMedia ID="L3dcd8ff8-678a-4e9f-8bb2-d6f3c35d1c10">
       <text>image description</text>
       <value mediaType="image/jpeg" xsi:type="ED">
          <reference value="Molecule2.jpg"/>
       </value>
    </observationMedia>
 </component>
</section>

以下是我获取每个id的值的方法:

for($i=0; $i < $pcount; $i++) //$pcount = no. of paragraphs in the xml 
{ 
    $reference_value =  (string) $component->section->component[$i]->observationMedia->value->reference['value'];
    $referencedObject = (string) $component->section->text->paragraph[$i]->renderMultiMedia['referencedObject'];
    $observationMedia_ID = (string) $component->section->component[$i]->observationMedia['ID'];
}

以下是原始的xml响应:pastebin.com/raw/fnWbifx0

1 个答案:

答案 0 :(得分:0)

我已经使用XPath来帮助查找相关数据。

我希望我已正确理解问题,每个renderMultiMedia都指向observationMedia个对象。这是我使用XPath确保找到相应数据的地方。您假设段落编号与媒体信息的顺序相匹配,这并不总是正确的。

<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );

$xml = simplexml_load_file("so.xml");
$xml->registerXPathNamespace("d", "urn:hl7-org:v3");
$rmElements = $xml->xpath("//d:renderMultiMedia");
$mediaData = [];
foreach ( $rmElements as $media)
{
    $referencedObject = (string)$media['referencedObject'];
    $mediaElement = $xml->xpath("//d:observationMedia[@ID='$referencedObject']")[0];
    $reference_value =  (string) $mediaElement->value->reference['value'];
    $observationMedia_ID = (string) $mediaElement['ID'];
    $mediaData[] = [ 'referencedObject' => $referencedObject,
            'reference_value' => $reference_value,
            'observationMedia_ID' => $observationMedia_ID
    ];

}
print_r($mediaData);

从代码中可以看出,我还将其更改为首先查找任何renderMultiMedia元素(d:,因为这会考虑使用默认命名空间设置registerXPathNamespace方法)然后为每个方法交叉引用referencedObject属性和observationMedia元素。

它构建了一个数组 - 但我希望您可以将其改为需要处理数据的方式。