【发布时间】:2021-06-28 15:41:47
【问题描述】:
我想将命名空间添加到我的 XML 中的特定父(和子)元素,所以它看起来像这样:
<list xmlns:base="http://schemas.example.com/base">
<base:customer>
<base:name>John Doe 1</base:name>
<base:address>Example 1</base:address>
<base:taxnumber>10000000-000-00</base:taxnumber>
</base:customer>
<product>
<name>Something</name>
<price>45.00</price>
</product>
</list>
我不知道如何将 base 命名空间添加到 customer 父元素。
这是我目前的代码:
header("Content-Type: application/xml");
$xml_string = "<list xmlns:base='http://schemas.example.com/base'/>";
$xml = simplexml_load_string($xml_string);
$xml->addChild("customer");
$xml->customer->addChild("name", "John Doe 1", "http://schemas.example.com/base");
$xml->customer->addChild("address", "Example 1", "http://schemas.example.com/base");
$xml->customer->addChild("taxnumber", "10000000-000-00", "http://schemas.example.com/base");
$xml->addChild("product");
$xml->product->addChild("name", "Something");
$xml->product->addChild("price", "45.00");
print $xml->saveXML();
有了这个,唯一缺少的是 customer 元素的 base 命名空间。
【问题讨论】:
标签: php xml namespaces simplexml parent