0

here is simple XML File and i need to get the value of unitCode and i am using DOMXPath object to get the values.

<cbc:ConsumerUnitQuantity unitCode="BX">
    80.000
</cbc:ConsumerUnitQuantity>

i have tried

$unitCode = $xpath->query('//cbc:ConsumerUnitQuantity [@unitCode=""]')->item(0);
1
  • Can you paste a more complete XML example with the cbc namespace definition? Commented Dec 18, 2013 at 11:01

1 Answer 1

1

You're missing the namespace. You XML elements have a namespace prefix, but you did not register one on the Xpath object. Check you document for a xmlns:cbc attribute.

$xml = <<<'XML'
<cbc:ConsumerUnitQuantity xmlns:cbc="your-namespace" unitCode="BX">
    80.000
</cbc:ConsumerUnitQuantity>
XML;

$dom = new DOMDocument();
$dom->loadXml($xml);

$xpath = new DOMXpath($dom);
$xpath->registerNamespace('ns-cbc', 'your-namespace');

var_dump(
  $xpath->evaluate('string(//ns-cbc:ConsumerUnitQuantity)', NULL, FALSE)
);

Unlike DOMXpath::query(), DOMXpath::evaluate() can return scalar values directly.

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

Comments

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.