0

This is my xml data.

<?xml version="1.0" encoding="UTF-8"?>
    <ns1:catalog
xmlns:ns1="http://www.omnichannelintegrationlayer.com/xml/catalog/2016-01-01" catalog-id="at-master-catalog">
<ns1:product product-id="4132002004">
    <ns1:min-order-quantity>1</ns1:min-order-quantity>
    <ns1:step-quantity>1</ns1:step-quantity>
    <ns1:short-description
        xmlns:ns2="xml" ns2:lang="de-AT">Jogginghose Cacy jr
    </ns1:short-description>
    <ns1:short-description
        xmlns:ns2="xml" ns2:lang="de-CH">Jogginghose Cacy jr
    </ns1:short-description>
</ns1:product>

I'm trying to filter the xml the short-description base on ns2:lang attribute.

This is what I've done so far:

foreach ($xml->xpath("//ns1:product[@product-id='".$productid."']/ns1:short-description/") as $short_description) {
 $namespaces = $short_description->getNameSpaces(true);
    $ns1        = $short_description->children($namespaces['ns1']);
    $ns2        = $short_description->children($namespaces['ns2']);

    var_dump($ns2);
    echo $ns2["lang"];
}

The output of var_dump looks okay:

object(SimpleXMLElement)#27 (1) { ["@attributes"]=> array(1) { ["lang"]=> string(5) "de-AT" } }

But I can't access the array because when I echo $ns2["lang"], I'm getting NULL.

I already tried different solution like declaring namespace first but no luck.

Thanks in advance.

12
  • can you show the complete code? Commented May 21, 2018 at 10:21
  • haven't you tried ->attributes()? Commented May 21, 2018 at 10:22
  • When I try attributes() I'm getting, Warning: var_dump(): Node no longer exists Commented May 21, 2018 at 10:23
  • @ChetanAmeta this is the whole code so far Commented May 21, 2018 at 10:23
  • 1
    Your XML doesn't have any tags named short-description - what are you expecting your xpath expression to match? Commented May 21, 2018 at 10:28

1 Answer 1

1

The values you are looking for are in the attributes and the attributes use a namespace which you can pass as a parameter to the attributes method.

The attribute itself is of type SimpleXMLElement and has a method __toString to get the text content that is directly in this element.

You could for example use echo $short_description->attributes($namespaces['ns2'])->lang; or cast it to a (string)

You might update your code to:

$namespaces = $xml->getNamespaces(true);
foreach ($xml->xpath("//ns1:product[@product-id='".$productid."']/ns1:short-description") as $short_description) {
    $langAsString = (string)$short_description->attributes($namespaces['ns2'])->lang;
    echo $langAsString . "<br>";
}

That would give you:

de-AT
de-CH

Demo

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

1 Comment

You didn't know how much help you did dude.. Thank you so much.

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.