使用Xpath按属性解析xml

时间:2022-12-01 10:55:36

I receive this XML like above:

我像上面一样收到这个XML:

<?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 ?

但我总是收到虚假的。正如你所看到的 - 我试图使用starts-with和contains但它不起作用。你能帮我吗 ?

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

第二个问题 - 可以使用没有注册命名空间的Xpath并使用如下的查询:'// x:field'(在xml中我只有 ) (...)>

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.

编辑:我更正了XML - 我在这里添加了错误的结束标记。这个xml只是准备好的例子,它不是我收到的真正的XML(一切都在抛光)。 $ entry商店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.

EDIT2:我检查了这个XML的模式 - 我发现字段和对象不是独占的 - 它可以存储许多节点。这是某种通用名称。

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.

现在我只有来自签名的字段而且我得到了所有属性(不仅仅是这个例子中的6个对象)。现在我必须为这些名称写一些mapper。

2 个解决方案

#1


1  

  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. 在像Bartosz回答的那样纠正关闭的xml标签后,你可以尝试使用xpath语法// field [@ object =“Name”],如果你不需要属性“object”是第一个。但我没有尝试用PHP。

  3. In a .NET-Application I use this without namespace registration and it works.
  4. 在.NET应用程序中,我使用它而没有命名空间注册,它可以工作。

#2


0  

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

我认为你的XML应该是这样的,我可以用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

将xmlns =“adress”更改为xmlns:bla =“adress”并且字段元素已用极点关闭

#1


1  

  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. 在像Bartosz回答的那样纠正关闭的xml标签后,你可以尝试使用xpath语法// field [@ object =“Name”],如果你不需要属性“object”是第一个。但我没有尝试用PHP。

  3. In a .NET-Application I use this without namespace registration and it works.
  4. 在.NET应用程序中,我使用它而没有命名空间注册,它可以工作。

#2


0  

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

我认为你的XML应该是这样的,我可以用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

将xmlns =“adress”更改为xmlns:bla =“adress”并且字段元素已用极点关闭