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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog