Xml analysis using Xpath by attributes

I get this XML as 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 get a lie. As you can see - I tried to use start-with and it contains, but this does not work. You can help?

The second question is - can I use Xpath without a namespace and using a query like: //// x: field '(in xml I only have <field (...)>)

EDIT: I fixed XML - here I put the wrong closing tags. This xml has just been prepared, it is not the real XML that I get (everything is polished). $ entry SimpleXMLElement.

EDIT2: I checked the schema of this XML. And I found out that the field and the object are not exclusive - it can store many nodes. This is a kind of generic name.

I changed my mind and I did wroten this:

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

, ( 6 , ). mapper .

+4
2
  • xml, Bartosz, xpath //field[@object="Name"], , "" . php.
  • .NET- .
+1

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

xmlns="adress" xmlns:bla="adress" pole

0
source

Source: https://habr.com/ru/post/1616685/


All Articles