Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中将XML名称空间添加到父元素_Php_Xml_Namespaces_Simplexml_Parent - Fatal编程技术网

如何在PHP中将XML名称空间添加到父元素

如何在PHP中将XML名称空间添加到父元素,php,xml,namespaces,simplexml,parent,Php,Xml,Namespaces,Simplexml,Parent,我想将名称空间添加到我的XML中的特定父(和子)元素中,因此看起来像这样: <list xmlns:base="http://schemas.example.com/base"> <base:customer> <base:name>John Doe 1</base:name> <base:address>Example 1</base:address> <

我想将名称空间添加到我的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>

无名氏1
例1
10000000-000-00
某物
45
我不知道如何将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();
标题(“内容类型:应用程序/xml”);
$xml_string=“”;
$xml=simplexml\u load\u字符串($xml\u字符串);
$xml->addChild(“客户”);
$xml->customer->addChild(“名称”、“John Doe 1”、”http://schemas.example.com/base");
$xml->customer->addChild(“地址”,“示例1”,”http://schemas.example.com/base");
$xml->customer->addChild(“taxnumber”,“10000000-000-00”,”http://schemas.example.com/base");
$xml->addChild(“产品”);
$xml->product->addChild(“名称”、“某物”);
$xml->product->addChild(“价格”,“45.00”);
打印$xml->saveXML();
这样一来,唯一缺少的就是customer元素的base名称空间。

有两种方式:

  • 将其用作默认名称空间
  • 将前缀添加到元素中
  • 但是,这可能会导致访问元素的语法不同。解决这个问题的简单方法是将创建的元素存储到变量中

    $xmlns_base = "http://schemas.example.com/base";
    
    $xml_string  = "<base:list xmlns:base='http://schemas.example.com/base'/>";
    
    $xml = simplexml_load_string($xml_string);
    
    $customer = $xml->addChild("base:customer", NULL, $xmlns_base);
    
    $customer->addChild("base:name", "John Doe 1", $xmlns_base);
    $customer->addChild("base:address", "Example 1", $xmlns_base);
    $customer->addChild("base:taxnumber", "10000000-000-00", $xmlns_base);
    
    // provide the empty namespace so it does not get added to the other namespace
    $product = $xml->addChild("product", "", "");
    
    $product->addChild("name", "Something");
    $product->addChild("price", "45.00");
    
    print $xml->saveXML();
    
    $xmlns\u base=”http://schemas.example.com/base";
    $xml_string=“”;
    $xml=simplexml\u load\u字符串($xml\u字符串);
    $customer=$xml->addChild(“base:customer”,NULL,$xmlns\u base);
    $customer->addChild(“base:name”,“John Doe 1”,“xmlns_base”);
    $customer->addChild(“base:address”,“示例1”,“$xmlns_base”);
    $customer->addChild(“基数:taxnumber”,“10000000-000-00”,“xmlns_基数”);
    //提供空名称空间,以便不会将其添加到其他名称空间
    $product=$xml->addChild(“产品”,“产品”,“产品”);
    $product->addChild(“名称”、“某物”);
    $product->addChild(“价格”,“45.00”);
    打印$xml->saveXML();