使用 PHP 将冒号写入 XML
Using PHP to write colons to XML
我正在尝试使用 SimpleXML 从 PhP 输出一个 XML 文件。我 运行 遇到了“:”(冒号)字符的问题。 (谈艺术模仿生活!)
有什么方法可以转义冒号以便我可以将我的元素添加到对象中吗?
这是我的代码:
$urlset->addAttribute('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9');
这一行没问题,所以只有属性名称失败,如下例所示:
$urlset->addAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
$urlset->addAttribute('xsi:schemaLocation','http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
在每种情况下,它都会删除“:”之前的所有内容,如下所示:
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
>
同样,我的问题不在于 reading/parsing 来自 XML 的“:”,而是来自 PHP 的将“:”写入 XML。网上有很多关于解析的内容,但我没有发现从 PHP.
写一个“:”
您似乎无法使用 SimpleXML 定义 将在文档中使用的名称space(xmlns
属性做)。我发现您可以像这样在根节点的声明中简单地指定它们:
$simpleXml = new SimpleXMLElement('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"></urlset>');
在构建站点地图的情况下,您可能不必担心设置除此之外的任何名称space。但是,对于更通用的解决方案,这定义了一个默认名称 space 和一个带有前缀 alt
.
的第二个名称 space
$simpleXml = new SimpleXMLElement('<root xmlns="http://default.namespace.com" xmlns:alt="http://alt.namespace.com"></root>');
$simpleXml->addChild("child", "node in the default namespace");
$simpleXml->addChild("other", "node in the alternate namespace", "http://alt.namespace.com");
print $simpleXml->asXML();
将产生:
<root xmlns="http://default.namespace.com" xmlns:alt="http://alt.namespace.com">
<child>node in the default namespace</child>
<alt:other>node in the alternate namespace</alt:other>
</root>
addAttribute
的第三个可选参数是名称space,它可以帮助您创建具有该名称的属性或节点space。注意需要使用名字space的url(不是前缀)
我正在尝试使用 SimpleXML 从 PhP 输出一个 XML 文件。我 运行 遇到了“:”(冒号)字符的问题。 (谈艺术模仿生活!)
有什么方法可以转义冒号以便我可以将我的元素添加到对象中吗?
这是我的代码:
$urlset->addAttribute('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9');
这一行没问题,所以只有属性名称失败,如下例所示:
$urlset->addAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
$urlset->addAttribute('xsi:schemaLocation','http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
在每种情况下,它都会删除“:”之前的所有内容,如下所示:
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
>
同样,我的问题不在于 reading/parsing 来自 XML 的“:”,而是来自 PHP 的将“:”写入 XML。网上有很多关于解析的内容,但我没有发现从 PHP.
写一个“:”您似乎无法使用 SimpleXML 定义 将在文档中使用的名称space(xmlns
属性做)。我发现您可以像这样在根节点的声明中简单地指定它们:
$simpleXml = new SimpleXMLElement('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"></urlset>');
在构建站点地图的情况下,您可能不必担心设置除此之外的任何名称space。但是,对于更通用的解决方案,这定义了一个默认名称 space 和一个带有前缀 alt
.
$simpleXml = new SimpleXMLElement('<root xmlns="http://default.namespace.com" xmlns:alt="http://alt.namespace.com"></root>');
$simpleXml->addChild("child", "node in the default namespace");
$simpleXml->addChild("other", "node in the alternate namespace", "http://alt.namespace.com");
print $simpleXml->asXML();
将产生:
<root xmlns="http://default.namespace.com" xmlns:alt="http://alt.namespace.com">
<child>node in the default namespace</child>
<alt:other>node in the alternate namespace</alt:other>
</root>
addAttribute
的第三个可选参数是名称space,它可以帮助您创建具有该名称的属性或节点space。注意需要使用名字space的url(不是前缀)