Reputation: 1519
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 protected]</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.
Upvotes: 4
Views: 85
Reputation: 51
//field[@object="Name"]
if you don't need that the attribute "object" is the first one. But I didn't tried it with php.Upvotes: 1
Reputation: 415
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 protected]</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
Upvotes: 0