medzbakh
medzbakh

Reputation: 43

Getting xml:id attribute using SimpleXML in PHP

I have this part of an XML file :

          <post xml:id="cmr-politweets-a445917194569134080" who="#cmr-politweets-p102397481"
        when="2014-03-18T14:38:38" xml:lang="fra">
        <p><distinct type="twitter-retweet"><ident>RT</ident>
            <addressingTerm><addressMarker>@</addressMarker><addressee type="twitter-account"
                ref="https://twitter.com/UDI_off 819772525"
            >UDI_off</addressee></addressingTerm>:</distinct>
              .<addressingTerm><addressMarker>@</addressMarker><addressee type="twitter-account"
              ref="#cmr-politweets-p102397481">Chantal_Jouanno</addressee></addressingTerm> : «
          La lutte contre la pédophilie ne peut fonctionner que par la dénonciation » =&gt; <ref
            target="http://t.co/qVd4gfPfs2 http://bit.ly/1dkQWKu"
          >http://t.co/qVd4gfPfs2</ref></p>
        ..................
      </post>

So, i want to get the xml:id using the simpleXML in PHP. Thanks

Upvotes: 0

Views: 559

Answers (1)

GGTT
GGTT

Reputation: 112

Supposing you have only one "xml:" prefixed attribute per element, and it is the xml:id, here is my solution :

function getXMLId(SimpleXMLElement $node) {
        if ($node) {
            if (count($node->attributes('xml', true)) == 1) {
                return $node->attributes('xml', true)[0];
            }
        }
        return '';
    }

Upvotes: 1

Related Questions