Reputation: 13
I try to get attrbiute and data from an XML with xPath. I cant' get attribute.
XML:
<table:table-row>
<table:table-cell table:style-name="Tabella1.A2" office:value-type="string">
<text:p text:style-name="Standard">RAG. SOC.:</text:p>
</table:table-cell>
<table:table-cell table:style-name="Tabella1.B2" office:value-type="string">
<text:p text:style-name="Standard">
<text:database-display text:table-name="portale.anaTanagrafica" text:table-type="table" text:column-name="ana_rag_sociale" text:database-name="Nuovo database"><ana_rag_sociale></text:database-display>
</text:p>
</table:table-cell>
</table:table-row>
and PHP function:
$path = "//text:database-display";
if ($xml !== FALSE) {
foreach($xml->xpath($path) as $agenzia) {
FB::INFO("Nodo: " . $agenzia);
FB::INFO("Nodo: " . $agenzia[@"text:table-name"]);
}
}
What I want is the following output:
Nodo: <ana_rag_sociale>
Nodo: portale.anaTanagrafica
ThankS!!
Upvotes: 1
Views: 188
Reputation: 60424
You need to register the namespace with SimpleXML
:
$xml->registerXPathNamespace('text', 'youractualtextnamespace');
Once you've done that, there are a few ways to access the data you want. Here is a complete example:
$string = <<<XML
<table:table-row xmlns:table="youractualnamespace"
xmlns:office="youractualofficenamespace" xmlns:text="youractualtextnamespace">
<table:table-cell table:style-name="Tabella1.A2"
office:value-type="string">
<text:p text:style-name="Standard">RAG. SOC.:</text:p>
</table:table-cell>
<table:table-cell table:style-name="Tabella1.B2"
office:value-type="string">
<text:p text:style-name="Standard">
<text:database-display text:table-name="portale.anaTanagrafica"
text:table-type="table" text:column-name="ana_rag_sociale"
text:database-name="Nuovo database"><ana_rag_sociale></text:database-display>
</text:p>
</table:table-cell>
</table:table-row>
XML;
$path = "//text:database-display";
$xml = new SimpleXMLElement($string);
$xml->registerXPathNamespace('text', 'youractualtextnamespace');
if ($xml !== FALSE) {
foreach($xml->xpath($path) as $agenzia) {
$attr = $agenzia->xpath("@text:table-name");
print("Nodo: " . $agenzia);
print("Nodo: " . $attr[0]);
}
}
Upvotes: 1