PHP5 Xpath problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    PHP5 Xpath problem

    Hi all,

    I have recently started working with the new XML functionality in
    PHP5, but I am running into a few problems. Specifically, I am using
    an Xpath query to try and pull out the data in specific elements
    within my XML file. The problem I am getting is that one of the XML
    elements contains namespace attributes and this seems to prevent the
    Xpath query from working (ie, if I manually strip the namespace
    attributes then the query works fine. After testing I will not have
    direct access to the XML file, so stripping the namespace attributes
    each time is not an option.

    Can anyone shed any light as to why this might be so, or offer any
    suggestions to overcome the problem?

    tia

    Chris

    ############### ############### #########
    Example code:

    <?
    $dom = new DomDocument();
    $dom->load("ltsnec.x ml");
    $path =
    "/OAI-PMH/ListRecords/record/metadata/lom/general/title/string";

    // XPath query
    $xp = new DomXpath($dom);
    $titles = $xp->query($path) ;

    $length = $titles->length;

    // this echoes 0 when the namespace attributes are present
    echo "<p>Number = " . $length . "</p>";

    foreach ($titles as $node) {
    echo "<p>:: " . $node->textContent . "</p>";
    }
    ?>

    Example XML which works

    <OAI-PMH>
    <ListRecords>
    <record>
    <header>

    <identifier>htt p://www.english.lts n.ac.uk/repository/lom/esc_100.xml</identifier>
    <datestamp>20 04-7-22</datestamp>
    </header>
    <metadata>
    <lom>
    <general>
    <identifier>htt p://purl.org/poi/english.ltsn.ac .uk/100</identifier>
    <title>
    <string language="en-GB">Universit y of Leeds - Bodington Common
    Sites</string>
    </title>
    <language>en-GB</language>
    <description>
    <string language="en-GB">Blah blah blah</string>
    </description>
    </general>

    ....etc...

    </lom></metadata></record></ListRecords></OAI-PMH>

    Example XML which doesn't work

    <OAI-PMH>
    <ListRecords>
    <record>
    <header>

    <identifier>htt p://www.english.lts n.ac.uk/repository/lom/esc_100.xml</identifier>
    <datestamp>20 04-7-22</datestamp>
    </header>
    <metadata>

    ## problem occurs when the lom tag includes the namespace attributes
    ## as follows

    <lom xmlns="http://ltsc.ieee.org/xsd/LOMv1p0"
    xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocat ion="http://ltsc.ieee.org/xsd/LOMv1p0
    http://www.rdn.ac.uk/oai/lom/lom.xsd">


    <general>
    <identifier>htt p://purl.org/poi/english.ltsn.ac .uk/100</identifier>
    <title>
    <string language="en-GB">Universit y of Leeds - Bodington Common
    Sites</string>
    </title>
    <language>en-GB</language>
    <description>
    <string language="en-GB">Blah blah blah</string>
    </description>
    </general>

    ....etc...

    </lom></metadata></record></ListRecords></OAI-PMH>
  • Adam Harvey

    #2
    Re: PHP5 Xpath problem

    On Wed, 18 Aug 2004 10:08:52 +0100, Chris wrote:[color=blue]
    > I have recently started working with the new XML functionality in
    > PHP5, but I am running into a few problems. Specifically, I am using
    > an Xpath query to try and pull out the data in specific elements
    > within my XML file. The problem I am getting is that one of the XML
    > elements contains namespace attributes and this seems to prevent the
    > Xpath query from working (ie, if I manually strip the namespace
    > attributes then the query works fine. After testing I will not have
    > direct access to the XML file, so stripping the namespace attributes
    > each time is not an option.
    >
    > Can anyone shed any light as to why this might be so, or offer any
    > suggestions to overcome the problem?[/color]

    I can't claim to have had huge success with XPath in PHP5 when dealing
    with XML Namespaces myself, but you can try calling the registerNamespa ce
    method of the $xp object with the namespace details before running any
    queries, provided you know what the namespaces are ahead of time (or don't
    mind looking the information up with something like getElementsByTa gNameNS.

    It's one of those things which at least seems like the right thing to do;
    hopefully we get a bit more documentation on it at some point.

    --
    Adam Harvey
    Optimiser Pty Ltd

    To e-mail: don't make an example out of me!

    Comment

    Working...