使用 php dom 生成 xml 命名空间

发布于 2025-01-01 06:04:45 字数 1031 浏览 0 评论 0原文

我在 XML、命名空间和 PHP DOM 方面遇到一些问题。

这是我应该得到的输出:

<cd:Document xmlns="http://www.zbs-giz.si/Schemas/2006/ZBSxml/2.2" xmlns:cd="http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0/ZbsCreaDoc.xsd">
<cd:Data>
    <cd:DataFormat>
        <cd:MimeType>text/xml</cd:MimeType>
    </cd:DataFormat>
    <cd:Content>
        <cd:EmbeddedData>

这是我的 PHP 代码

$root = $doc->appendChild($doc->createElementNS('http://www.zbs-giz.si/Schemas/2006/ZBSxml/2.2', 'cd:Document'));
$root->setAttributeNS('http://www.zbs-giz.si/Schemas/2006/ZBSxml/2.2', 'cd', 'http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0');
$root->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xsi:schemaLocation', 'http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0/ZbsCreaDoc.xsd');

有什么想法如何解决这个问题吗?

I have some problem with XML, namespace and PHP DOM.

This is my output that I should get:

<cd:Document xmlns="http://www.zbs-giz.si/Schemas/2006/ZBSxml/2.2" xmlns:cd="http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0/ZbsCreaDoc.xsd">
<cd:Data>
    <cd:DataFormat>
        <cd:MimeType>text/xml</cd:MimeType>
    </cd:DataFormat>
    <cd:Content>
        <cd:EmbeddedData>

and this is my PHP code

$root = $doc->appendChild($doc->createElementNS('http://www.zbs-giz.si/Schemas/2006/ZBSxml/2.2', 'cd:Document'));
$root->setAttributeNS('http://www.zbs-giz.si/Schemas/2006/ZBSxml/2.2', 'cd', 'http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0');
$root->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xsi:schemaLocation', 'http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0/ZbsCreaDoc.xsd');

Any ideas how to resolve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如何视而不见 2025-01-08 06:04:45
<?php
$doc = new DOMDocument();
$doc->formatOutput = true;
//set root element to correct cd prefix _and_ namespace:
$root = $doc->appendChild(
        $doc->createElementNS(
        $cd = 'http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0',
        'cd:Document'));
//this is the bit of obscure magic: it will set the default namespace
$doc->createAttributeNS(
        'http://www.zbs-giz.si/Schemas/2006/ZBSxml/2.2',
        'xmlns');
//now continue as normal
$root->setAttributeNS(
        'http://www.w3.org/2001/XMLSchema-instance',
        'xsi:schemaLocation',
        'http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0/ZbsCreaDoc.xsd');
$data = $root->appendChild($doc->createElementNS($cd,'cd:Data'));
$dataformat = $data->appendChild($doc->createElementNS($cd,'cd:DataFormat'));
$dataformat->appendChild($doc->createElementNS($cd,'cd:MimeType','text/xml'));
$content = $data->appendChild($doc->createElementNS($cd,'cd:Content'));
<?php
$doc = new DOMDocument();
$doc->formatOutput = true;
//set root element to correct cd prefix _and_ namespace:
$root = $doc->appendChild(
        $doc->createElementNS(
        $cd = 'http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0',
        'cd:Document'));
//this is the bit of obscure magic: it will set the default namespace
$doc->createAttributeNS(
        'http://www.zbs-giz.si/Schemas/2006/ZBSxml/2.2',
        'xmlns');
//now continue as normal
$root->setAttributeNS(
        'http://www.w3.org/2001/XMLSchema-instance',
        'xsi:schemaLocation',
        'http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0/ZbsCreaDoc.xsd');
$data = $root->appendChild($doc->createElementNS($cd,'cd:Data'));
$dataformat = $data->appendChild($doc->createElementNS($cd,'cd:DataFormat'));
$dataformat->appendChild($doc->createElementNS($cd,'cd:MimeType','text/xml'));
$content = $data->appendChild($doc->createElementNS($cd,'cd:Content'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文