0

I am using PHP DOM XPATH. I have XML structure as below

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:rank="http://www.w3.org/2001/XMLSchema">
    <rank:one year = "2015">99</<rank:one>
    <rank:two year = "2014">128</rank:two>
    <rank:three year = "2015">27</rank:three>
</root>

where I want to get name and value of node where year is 2015

expected output is rank:one -- 99 and rank:three -- 27 I tried to write xpath query as

$xpath->query('//root/[@year="2015"]');

I am not getting what should be xpath expression in this case

1 Answer 1

1

The XPath expression should be

$xpath->query('//root/*[@year="2015"]');

Then you can use name() and text() to get its name and value, respectively.

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

2 Comments

Thanks. Exactly what I want.
please do not reject suggested edits like stackoverflow.com/review/suggested-edits/17296854. We're trying to improve StackOverflow, so even minor good edits should be approved. Otherwise you're leaving undesirable noise on the post. To judge if it is desirable, you could ask yourself "would I approve a suggestion to *add* thank you?" Alternatively, if you really want to decline someone for a "too minor" edit (which, again, you shouldn't), then use "reject and edit" and re-apply the minor edit to avoid leaving the noise on the post.

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.