Accessing the attributes of XML elements with namespaces

Discussion in 'XML' started by matthewjbarr@gmail.com, Feb 10, 2007.

  1. Guest

    Sorry for cross-posting, but I posted this in the PHP group and think
    it probably should have been here - think maybe I should be looking at
    XPath expressions to solve the problem. E.g. something like this:

    $item->xpath("//movie:image@src")

    ***

    Hi there,

    I'm using PHP5's SimpleXML and the simplexml_load_string() method to
    read some XML files.

    I can loop round all the elements and extract data from the elements
    using the usual syntax e.g.

    foreach ($xml->list->item as $myitem) { print($myitem->title); }

    However, I don't know how to access the value of an attribute where
    the element has a namespace associated with it, e.g.

    <item>
    <movie:title>Star Wars</movie:title>
    <movie:image src="anImage.jpg" alt="Star Wars" />
    </item>

    How do I get the value of the src attribute, i.e. "anImage.jpg"?

    Thanks!

    ***
     
    , Feb 10, 2007
    #1
    1. Advertising

  2. wrote:
    > However, I don't know how to access the value of an attribute where
    > the element has a namespace associated with it


    In XPath 1.0, the normal solution is to use a prefix in the path -- then
    use a separate (and unfortunately implementation-specific!) mechanism to
    tell the Xpath interpreter what namespace is associated with that path.

    (If the XPath is within an XSLT stylesheet, the mechanism is simply to
    make sure this prefix is defined at that point in the stylesheet. If
    you're just using standalone XPath, check the specs for your XPath engine.)

    If you need a portable namespace-aware XPath, there is a kluge-around
    that involves matching on * and then using a predicate to check both the
    localname and the namespace URI. Ugly, but sometimes unavoidable.



    I don't do PHP, so I can't advise you on what the structure-like syntax
    would be to access a namespaced node. There probably is one; Read The
    Fine Manual... Of course the standard DOM API, which is what most
    languages use for this purpose, handles namespaces with no problem.

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Feb 10, 2007
    #2
    1. Advertising

  3. wrote:


    > However, I don't know how to access the value of an attribute where
    > the element has a namespace associated with it, e.g.
    >
    > <item>
    > <movie:title>Star Wars</movie:title>
    > <movie:image src="anImage.jpg" alt="Star Wars" />
    > </item>
    >
    > How do I get the value of the src attribute, i.e. "anImage.jpg"?


    Here is an example, tested with PHP 5.2

    $xml = <<<END
    <item xmlns:movie="http://example.com/2007/movie">
    <movie:title>Star Wars</movie:title>
    <movie:image src="anImage.jpg" alt="Star Wars" />
    </item>
    END;

    header('Content-Type: text/plain');

    $item = new SimpleXMLElement($xml);

    $item->registerXPathNamespace('mv', 'http://example.com/2007/movie');

    foreach ($item->xpath('mv:image') as $image) {
    echo $image['src'] . "\r\n";
    }

    outputs "anImage.jpg".


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 11, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wooks
    Replies:
    1
    Views:
    509
  2. kj
    Replies:
    2
    Views:
    394
    C. M. Sperberg-McQueen
    Apr 23, 2004
  3. Replies:
    2
    Views:
    412
  4. Gerald Aichholzer
    Replies:
    2
    Views:
    2,522
    Gerald Aichholzer
    Jun 27, 2006
  5. johkar
    Replies:
    3
    Views:
    642
    johkar
    Mar 13, 2008
Loading...

Share This Page