前缀命名空间已删除

时间:2015-11-16 15:19:58

标签: php xml

我想将一些发票信息导出到xml文件中。 这就是它应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<Invoice xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:udt="urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ccts="urn:oasis:names:specification:ubl:schema:xsd:CoreComponentParameters-2" xmlns:stat="urn:oasis:names:specification:ubl:schema:xsd:DocumentStatusCode-1.0" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
  <cbc:UBLVersionID>2.1</cbc:UBLVersionID>

这是我正在使用的代码:

<?php
    $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><Invoice/>');
    $xml->addAttribute('xmlns', 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2');
    $xml->addAttribute('xmlns:qdt', 'urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2');
    $xml->addAttribute('xmlns:cbc', 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2');
    $xml->addAttribute('xmlns:udt', 'urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2');
    $xml->addAttribute('xmlns:cac', 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2');
    $xml->addAttribute('xmlns:ccts', 'urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2');
    $xml->addAttribute('xmlns:udt', 'urn:oasis:names:specification:ubl:schema:xsd:CoreComponentParameters-2');
    $xml->addAttribute('xmlns:stat', 'urn:oasis:names:specification:ubl:schema:xsd:DocumentStatusCode-1.0');


    $xml->addChild('cbc:UBLVersionID', '2.1'); ?>

但是当我运行php代码时,前缀不再存在了。我该如何解决这个问题?

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" udt="urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2" cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" ccts="urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2" stat="urn:oasis:names:specification:ubl:schema:xsd:DocumentStatusCode-1.0">
<UBLVersionID>2.1</UBLVersionID>

2 个答案:

答案 0 :(得分:0)

您需要将名称空间作为第三个参数:

$xml->addAttribute('qdt',
    'urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2',
    'xmlns');

// some lines omitted...

$xml->addChild('UBLVersionID', '2.1', 'cbc');

答案 1 :(得分:-1)

您不会将属性本身添加到正确的命名空间。

xmlns属性定义默认命名空间。该属性本身没有名称空间。

所有xmlns:*属性都是其他命名空间定义。他们使用保留的命名空间http://www.w3.org/2000/xmlns/

因此,如果invoice是默认命名空间的一部分,则应将该定义移动到模板XML中。

$xml = new SimpleXMLElement(
  '<?xml version="1.0" encoding="UTF-8"?><Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"/>'
);
$xml->addAttribute(
  'xmlns:qdt',      
  'urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2',
  'http://www.w3.org/2000/xmlns/'
);

echo $xml->asXml();

输出:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:xmlns="http://www.w3.org/2000/xmlns/" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"/>

但是您不需要手动添加命名空间属性,如果向命名空间添加元素,则会根据需要添加定义:

$xmlns = [
  'cbc' => 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'
];
$xml = new SimpleXMLElement(
  '<?xml version="1.0" encoding="UTF-8"?><Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"/>'
);

$xml->addChild('cbc:UBLVersionID', '2.1', $xmlns['cbc']);

echo $xml->asXml();

输出:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
  <cbc:UBLVersionID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2.1</cbc:UBLVersionID>
</Invoice>

手动添加属性可以避免在兄弟姐妹中对同一命名空间进行多次定义。但这只是一种优化。