如何使用SimpleXML加载所有xml:lang属性

时间:2014-01-10 05:38:50

标签: php xml simplexml lang

如何阅读所有属性xml:lang值? 有时我不知道XML数据中定义了多少种语言。

<?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>

我可以通过在xpath

中添加确切的语言代码来xml:lang parse in PHP

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

但我需要将所有xml:lang atributes值添加到已解析的数组中。

可以用PHP SimpleXML完成吗?

3 个答案:

答案 0 :(得分:1)

我不是100%对SimpleXML抱歉,但我知道DomDocument可以做你想做的事。希望这对您有用:

$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 />';
}

您可以将$ node-&gt; getAttribute('xml:lang')分配给变量并运行一些检查以查看它是否匹配'eng'或您需要的任何内容。

我使用了原始帖子中的xpath,但您也可以使用$ dom-&gt; getElementsByTagName('name')并以相同的方式访问值和属性。

答案 1 :(得分:1)

怎么样:

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

将返回<name>个节点的数组。

如果不是这样,请准确说明您想要的结果。

修改

尝试此操作仅获取xml:lang属性:

$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);

答案 2 :(得分:0)

我发现了访问命名空间属性的简便方法。您可以使用$name->attributes("xml", true)函数。

这是工作示例:

<?php 
  
$xmlString = '
<products> 
  <product>
    <name xml:lang="eng">
      Apples
    </name>
    <name xml:lang="fr">
      Pommes
    </name>
  </product>
  <product>
    <name xml:lang="eng">
      Strawberries
    </name>
    <name xml:lang="fr">
      Fraises
    </name>
  </product>
</products> 
'; 

$xml = new \SimpleXMLElement($xmlString); 

foreach($xml->product as $product) 
{ 
  foreach ($product->name as $name) 
  {
    $attributes = $name->attributes("xml", true);
    // print_r($attributes);

    foreach ($attributes as $attributeName => $attributeValue)
    {
      // echo $attributeName . PHP_EOL;
      // echo $attributeValue . PHP_EOL;

      if ($attributeValue == "eng" && $attributeName == "lang") {
        echo "English: " . trim(((string) $name)) . PHP_EOL;
      }
      if ($attributeValue == "fr" && $attributeName == "lang") {
        echo "French: " . trim(((string) $name)) . PHP_EOL;
      }
    }
  }
} 

在线演示:https://repl.it/repls/LovingFineDaemon