1

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">&lt;ana_rag_sociale&gt;</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: &lt;ana_rag_sociale&gt;
Nodo: portale.anaTanagrafica

ThankS!!

1
  • please post the namespace-prefix of the XML as well Commented Jan 17, 2014 at 17:15

1 Answer 1

1

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">&lt;ana_rag_sociale&gt;</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]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.