使用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.

dongyan6503
dongyan6503 为什么用/pole关闭字段标签?最好检查一下你是否已成功加载XML>php.net/manual/en/simplexml.examples-errors.php
接近 5 年之前 回复

2个回答




  1. 在像Bartosz回答的那样纠正关闭的xml标签之后你可以试试xpath语法 // field [ @ object =“Name”] </ code>如果你不需要属性“object”是第一个。 但是我没有尝试使用php。</ li>
  2. 在.NET-Application中我使用它而没有命名空间注册,它可以工作。</ li>
    </ ol>
    </ div >

展开原文

原文

  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.

doulutian4843
doulutian4843 你的解决方案很棒。 但是我在EDIT2中写道 - 由于这些通用名称,它抓得太多了......但是你的解决方案帮我准备了另一个解决方案。
接近 5 年之前 回复

I think your XML should go like this, with it I could parse it with PHP XPath

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="nameOwn.xsl"?>
<sawpe xmlns:bla="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>

Changed xmlns="adress" to xmlns:bla="adress" And field element was closed with pole

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问