0

I have searched high and low and I can't find a straight answer to my situation. I have the following XML:

<Tims xmlns="http://my.url.com/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://my.url.com/namespace http://my.url.com/xsd/Tims.xsd">
<Database xlink:href="http://my.url.com/xml/performance/database.svc">
    <ProjectID xlink:href="http://my.url.com/xml/Tvq11p/project.svc" title="Title1">Tvq11p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq10p/project.svc" title="Title2">Tvq10p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq8p/project.svc" title="Title3">Tvq8p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq9p/project.svc" title="Title4">Tvq9p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq5p/project.svc" title="Title5">Tvq5p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq15p/project.svc" title="Title6">Tvq15p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq2p/project.svc" title="Title7>Tvq2p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq3p/project.svc" title="Title8">Tvq3p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq12p/project.svc" title="Title9">Tvq12p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq13p/project.svc" title="Title10">Tvq13p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq14p/project.svc" title="Title11">Tvq14p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq1p/project.svc" title="Title12">Tvq1p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq4p/project.svc" title="Title13">Tvq4p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq6p/project.svc" title="Title14">Tvq6p</ProjectID>
    <ProjectID xlink:href="http://my.url.com/xml/Tvq7p/project.svc" title="Title15">Tvq7p</ProjectID>
</Database>
<Timestamp>2012-08-03T09:47:06-04:00</Timestamp>
<ExecutionTime>0.01</ExecutionTime>
</Tims>

All I want to do is extract each ProjectID node's attributes and value..

I've tried DOM, SimpleXML, and XMLParser. This is all in PHP, however, I am actually open to alternative languages as long as they can be executed from a Unix command line.

Thanks!

UPDATE What I've tried: I've mostly deleted my attempts as I try and fail. My latest iteration is this:

$return = send_request($requestUri, TIMS_REQUEST_TIMEOUT); // returns SimpleXMLElement
$return->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');
foreach($return->xpath('//ProjectID') as $project) {
    $project->registerXPathNamespace('xlink', 'http://www.w3.org/1999/xlink');
    print_r($project);
}

My print_r never fires.

5
  • 1
    Your title7 attribute is missing a closing quote. Is that your problem ? Commented Aug 3, 2012 at 14:45
  • Your Tims element should be self-closing as well. Commented Aug 3, 2012 at 14:49
  • Sorry, poor copy/paste... updated. Commented Aug 3, 2012 at 14:51
  • Here's a horrible, but working, solution. Commented Aug 3, 2012 at 15:01
  • To reiterate @BrianAgnew's comment, title7 is missing a close quote. If that wasn't from a bad copy/paste, that's likely your problem. Commented Aug 3, 2012 at 15:02

1 Answer 1

1

You don't need to use XPath for such a simple XML schema. Try this:

foreach( $xml->Database->ProjectID as $el) {
    $attributes = $el->attributes( 'xlink', true);
    echo $attributes['href'] . ' ' . $el . "\n";
}

This is looping all of the ProjectID nodes and grabbing the href attribute and printing the contents of the node. This worked for me after fixing the missing quote in the title="Title7 attribute.

Sign up to request clarification or add additional context in comments.

1 Comment

@Honus Not a problem! I'm glad I could help.

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.