dry9192 2015-11-20 07:41
浏览 78
已采纳

使用Xpath按属性解析xml

I receive this XML like above:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="nameOwn.xsl"?>
<sawpe xmlns="adress" xmlns:xsi="secondadress">
<raport>
      <dataTS>2014-09-09 15:12:47</dataTS>
      <files>
        <file>name.xml</file>
      </files>
      <signature>
        <field object="E-mail (EMAILADDRESS)">email@email.com</field>
        <field object="Subject (CN)">Name Surname</field>
        <field object="Country (C)">PL</field>
        <field object="Name (GIVENNAME)">Name</field>
        <field object="Surname (SURNAME)">Surname</field>
        <field object="Number (SERIALNUMBER)">SERIALNUMBER:32106901960</field>
    </signature>
  </raport>
</sawpe>

I wrote:

$domInternal = new SimpleXMLElement($this->xml, LIBXML_COMPACT);
$namespaces = $domInternal->getNamespaces(true);
$domInternal->registerXPathNamespace('x',$namespaces['']);
$informationAboutSignature = $domInternal->xpath('//x:raport/x:signature');

foreach($informationAboutSignature as $entry){
    $person['name'] = $entry->xpath('//x:field[contains(@object, "Name")]');
    $person['surname'] = $entry->xpath('//x:field[contains(@object, "Surname")]');
    $person['serialNumber'] = $entry->xpath('//x:field[starts-with(@object, "Number")]');
    $person['country'] = $entry->xpath('//x:field[starts-with(@object, "Country")]');
    $person['contact'] = $entry->xpath('//x:field[starts-with(@object, "E-mail")]');
}

But I always receive false. As you can see - I tried to use starts-with and contains but it isn't works. Can you help ?

Second question - its possible to use Xpath without registration namespace and using query like: '//x:field' (in xml I have just <field (...)>)

EDIT: I corrected XML - I put incorrect closing tags here. This xml is just prepared example, it isn't real XML which I receive (everything is in polish). $entry store SimpleXMLElement.

EDIT2: I checked schema of this XML - And I found out that field and object isn't exclusive - it's can store many nodes. It's some kind of generic name.

I changed my solution and I've wroten this:

foreach($domInternal->raport->signature->field as $field){
        $attribute = (string)$field->attributes();
        $value = (string)$field[0];
}

Now I have only field from signature AND I got every attributes (not only these 6 object like in example). Now I have to write some mapper for these name.

  • 写回答

2条回答 默认 最新

  • dongwu9972 2015-11-20 08:47
    关注
    1. After correcting the closing xml tag in like Bartosz answered you can try the xpath syntax //field[@object="Name"] if you don't need that the attribute "object" is the first one. But I didn't tried it with php.
    2. In a .NET-Application I use this without namespace registration and it works.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题