Question

How to read ALL atribute xml:lang values? Sometimes I do not know how many languages are defined in XMLs data.

<?xml version="1.0" encoding="UTF-8"?>
<offer>
  <products>
    <product>
      <description>
        <name xml:lang="eng">English translation</name>
        <name xml:lang="lat">Latvian translation</name>
      </description>
    </product>
    <product>
      <description>
        <name xml:lang="eng">The same English</name>
        <name xml:lang="pol">And Polish language</name>
      </description>
    </product>
  </products>
</offer>

I can xml:lang parse in PHP by adding exact language code in xpath

print_r($xml->xpath('products/product/description/name[@xml:lang = "eng"]'));

But I need to add all xml:lang atributes values to parsed array.

Can it be done with PHP SimpleXML?

Was it helpful?

Solution

what about this:

$nodes = $xml->xpath('products/product/description/name[@xml:lang]');

Will return an array of <name>-nodes.

If this is not it, please clarify exactly your desired result.

EDIT

try this to get the xml:lang attributes only:

$langs = $xml->xpath("products/product/description/name[@xml:lang]/@xml:lang");
// $lang is an array of simplexml-elements, transform the values to string like this:
$langs = array_map("strval", $langs);

OTHER TIPS

I'm not 100% on SimpleXML sorry, but I know DomDocument can do what you are after. Hopefully this can be of use to you:

$xmlstring = '<?xml version="1.0" encoding="UTF-8"?>
<offer>
  <products>
    <product>
      <description>
        <name xml:lang="eng">English translation</name>
        <name xml:lang="lat">Latvian translation</name>
      </description>
    </product>
    <product>
      <description>
        <name xml:lang="eng">The same English</name>
        <name xml:lang="pol">And Polish language</name>
      </description>
    </product>
  </products>
</offer>';

$dom = new DOMDocument();
$dom->loadXML($xmlstring); //or $dom->load('filename.xml');

$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//products/product/description/name');

foreach ($nodes as $node) {
    echo 'Language: ' . $node->getAttribute('xml:lang') . '<br />';
    echo 'Value: ' . $node->nodeValue . '<br /><br />';
}

You can assign $node->getAttribute('xml:lang') to a variable and run some checks to see if it matches 'eng' or whatever you need.

I used xpath as you had in your original post, but you can also use $dom->getElementsByTagName('name') and access values and attributes in much the same way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top