douxian8883 2014-05-28 11:02
浏览 41

如何使用PHP XMLWriter向writeAttributeNS添加属性

I'm newbie in XML-writer. Any idea how to generate this with PHP XMLWriter

<definitions xmlns:tns="urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport">

I've done this already

$writer->startElement('definitions');
$writer->writeAttributeNS('xmlns','tns', null,'urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport');
$writer->writeAttributeNS('xmlns',null, null, 'http://schemas.xmlsoap.org/wsdl/');
$writer->writeAttributeNS('targetNamespace', null, null, 'urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport');

But I get this "Warning: XMLWriter::writeAttributeNs(): Invalid Attribute Name in" 3'rd and 4'th lines

Thanks already! JM

  • 写回答

1条回答 默认 最新

  • doujianjian2060 2014-05-28 19:59
    关注

    Here is only one "normal" attribute in your xml. An this is the targetNamespace attribute. The other two are namespace (prefix) definitions

    The xmlns attribute is the default namespace definition for the element and its child elements. It will be created automatically if you use startElementNS().

    The targetNamespace attribute has no namespace and can be set using writeAttribute().

    Last xmlns:tns is not needed in the example, it defines the namespace prefix "tns" for child elements and attibutes. Here is no element or attribute that uses it. If you add it with setAttributeNS(), XMLWriter will add the xmlns namespace, too. This is a reserved namespace (and namespace prefix). It is builtin into XML parsers and does not need to be defined, but it can.

    $writer = new XMLWriter;
    $writer->openURI('php://output');
    $writer->startDocument('1.0', 'UTF-8');
    $writer->startElementNS(
      NULL,
      'definitions',
      'http://schemas.xmlsoap.org/wsdl/'
    );
      $writer->writeAttribute(
        'targetNamespace',
        'urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport'
      );
      $writer->writeAttributeNS(
        'xmlns',
        'tns',
        'http://www.w3.org/2000/xmlns/',
        'urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport'
      );
    $writer->endElement();
    $writer->endDocument();
    

    Output:

    <definitions targetNamespace="urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport" xmlns:tns="urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport" xmlns:xmlns="http://www.w3.org/2000/xmlns/" xmlns="http://schemas.xmlsoap.org/wsdl/"/>
    

    Here is an example for a namespace attribute, like with startElementNS() the namespace definition will be added automatically:

    $writer->startElement('element');
      $writer->writeAttributeNS(
        'prefix',
        'attr',
        'urn:example:namespace',
        'value'
      );
    $writer->endElement();
    

    Output:

    <?xml version="1.0" encoding="UTF-8"?>
    <element prefix:attr="value" xmlns:prefix="urn:example:namespace"/>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算