简体   繁体   English

PHP用正确的名称空间和模式位置编写XML

[英]PHP Write XML with correct namespace and schema location

I am writing XML using XMLWriter. 我正在使用XMLWriter编写XML。 I've been able to create 我已经能够创建

<foo xmlns="https://www.example.com" id="1" name="test" />

With: 附:

$w=new XMLWriter();
$w->openMemory();
$w->startDocument('1.0','UTF-8');
$w->startElementNS(null, "foo", "https://www.example.com");
$w->writeAttribute("id", 1);
$w->writeAttribute("name", "test");
$w->endDocument();
return $w->outputMemory(true);

However, what I need is to add xsi and xsi:schemaLocation, such as: 但是,我需要添加xsi和xsi:schemaLocation,例如:

<foo xmlns="https://www.example.com" id="1" name="test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.example.com/myschema.xsd" />

I tried using this code: 我尝试使用此代码:

$w->writeAttributeNS("xmlns","xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/2001/XMLSchema-instance");

But then I end up with: 但是最后我得到了:

<foo xmlns="https://www.example.com" id="1" name="test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xmlns="http://www.w3.org/2001/XMLSchema-instance" />

Which isn't valid, nor is it what I need. 这是无效的,也不是我所需要的。

I figured it out, just use writeAttribute instead of writeAttributeNS . 我想通了,只需使用writeAttribute而不是writeAttributeNS

$w->writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
$w->writeAttribute("xsi:schemaLocation", "https://www.example.com/myschema.xsd");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM