【问题标题】:PHP how to Create special Element <eanucc:countryISOCode xmlns:eanucc="urn:ean.ucc:2">PHP 如何创建特殊元素 <eanucc:countryISOCode xmlns:eanucc="urn:ean.ucc:2">
【发布时间】:2018-10-18 08:59:34
【问题描述】:

我正在尝试在 php 中创建以下元素,以创建我们的一位客户所需的 xml。

一切正常,但我不知道如何创建以下元素

我尝试了几种方法,但仍然无法正确生成上述示例

有人可以帮我上路吗?

这就是我在编码中所拥有的

$eanucc_countryISOCode = $dom->createElement('eanucc','BE');
$eanucc_countryISOCode->setAttribute('xmlns', 'urn:ean.ucc:2');

【问题讨论】:

标签: php xml


【解决方案1】:

xmlns:eanucc 是命名空间urn:ean.ucc:2 的命名空间定义。使用命名空间感知 DOM 方法(后缀为 NS)。

$document = new DOMDocument();
$document
  ->appendChild(
    $document->createElementNS('urn:ean.ucc:2', 'eanucc:countryISOCode')    
  )
  ->appendChild(
    $document->createTextnode('BE')    
  );

echo $document->saveXML();

输出:

<?xml version="1.0"?>
<eanucc:countryISOCode xmlns:eanucc="urn:ean.ucc:2">BE</eanucc:countryISOCode>

【讨论】:

    【解决方案2】:

    这个语法是正确的!

    $eanucc_countryISOCode = $doc->createElement('eanucc:countryISOCode','BE');
    $eanucc_countryISOCode->setAttribute('xmlns:eanucc', 'urn:ean.ucc:2');
    ...->appendChild( $eanucc_countryISOCode );
    

    结果:

    <eanucc:countryISOCode xmlns:eanucc="urn:ean.ucc:2">BE</eanucc:countryISOCode>
    

    【讨论】:

    • 你好 Yona,谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多