doutao5419 2018-07-17 20:54
浏览 50

SimpleXMLElement在单个元素中有两个名称空间

I'm trying to make this xml using SimpleXMLElement:

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:etsi="http://uri.etsi.org/01903/v1.3.2#" Id="Signature620397">

 $signature = $invoice->addChild('ds:Signature', null, 'http://www.w3.org/2000/09/xmldsig#');
 $signature->addAttribute('xmlns:etsi', 'http://uri.etsi.org/01903/v1.3.2#');
 $signature->addAttribute('Id', 'Signature620397');

Later in the xml i need to reference the etsi namespace, but i could not find a way

<etsi:QualifyingProperties Target="#Signature620397">

Is there anyway to do this? Or that would be the limitations of SimpleXMLElement

  • 写回答

1条回答 默认 最新

  • duanaoreng9355 2018-07-19 15:17
    关注

    You might find this related answer useful.

    A namespace is ultimately identified only by its full URI; the etsi prefix might change meaning halfway down the document. The way SimpleXML has been designed, you have to mention this full URI every time you add a namespaced element or attribute.

    On the other hand, you don't need to manually add the xmlns:etsi attribute; SimpleXML will add that automatically when you first use the namespace.

    In other words, you have to write:

     $xml->addChild('etsi:QualifyingProperties', null, 'http://uri.etsi.org/01903/v1.3.2#');
    

    And SimpleXML will generate this if it's the first time you've mentioned that namespace:

    <etsi:QualifyingProperties xmlns:etsi="http://uri.etsi.org/01903/v1.3.2#" />
    

    Or just this if the prefix etsi is already defined with the right value at that point in the document:

    <etsi:QualifyingProperties />
    

    Obviously, it's tedious to write out the full URI over and over, but that can be resolved simply by having a variable or constant in your code with a name that means something to you, like this:

    define('XMLNS_ETSI_132', 'http://uri.etsi.org/01903/v1.3.2#');
    $xml->addChild('etsi:QualifyingProperties', null, XMLNS_ETSI_132);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100