在节点中使用 xmlns:xlink 属性创建 XML [英] Create XML with xmlns:xlink attribute in a node

查看:39
本文介绍了在节点中使用 xmlns:xlink 属性创建 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加生成这样的输出:

i'm trying to add generate an output like this:

<mets ....
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.loc.gov/METS/       
http://www.loc.gov/standards/mets/mets.xsd">

我可以正常生成所有内容,但无法添加 xmlns:xlink 属性.我得到的最接近的是:

I can generate everything fine, but cannot add the xmlns:xlink attribute. The closest I get is:

$this->xml = new SimpleXMLElement('<mets></mets>');
$mets->addAttribute("xlink:someName", "blabla", "http://www.w3.org/1999/xlink");    
$mets->addAttribute("xsi:schemaLocation", "http://www.loc.gov/METS/  
http://www.loc.gov/standards/mets/mets.xsd",
"http://www.w3.org/2001/XMLSchema-instance");

生成:

<mets ....
xmlns:xlink="http://www.w3.org/1999/xlink"
----begin of part I don't desire-----
xlink:someName="blablabla"
----end of  part I don't desire-----
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.loc.gov/METS/       
http://www.loc.gov/standards/mets/mets.xsd">

如何在不添加xlink:somethingElse的情况下添加xmlns:xlink?

How can I add xmlns:xlink without adding xlink:somethingElse?

推荐答案

我提出的解决方案非常简单:

The solution I've come up with is rather straight forward:

因为

$mets->addAttribute("xlink:someName", "", "http://www.w3.org/1999/xlink");

将始终添加两个属性 - 一个用于命名空间声明 (xmlns:xlink),然后是您实际添加的属性 (xlink:someName) - 所有您需要的然后删除不需要的添加属性,前缀命名空间属性将保留:

will always add two attributes - one for the namespace declaration (xmlns:xlink) and then the attribute you actually add (xlink:someName) - all you need to do is then to remove the unwanted added attribute and the prefix namespace attribute will remain:

unset($mets->attributes('xlink', true)['someName']);

完整示例:

$mets = new SimpleXMLElement('<mets></mets>');
$mets->addAttribute("xlink:someName", "", "http://www.w3.org/1999/xlink");
unset($mets->attributes('xlink', true)['someName']);
echo $mets->asXML();

输出:

<?xml version="1.0"?>
<mets xmlns:xlink="http://www.w3.org/1999/xlink"/>

然而,这通常不是必要的.您要么需要为某事使用命名空间 - 然后 simplexml 会在需要时添加它 - 或者您不需要它,那么就不需要添加它.

However this normally should not be necessary. You either need to use the namespace for something - then simplexml will add it when needed - or you don't need it, then there is no need to add it.

XML 本身根本不需要声明一个未使用的命名空间.因此,您可能也可以将其省略,或者您只需要在需要添加它的地方添加它,例如稍后将使用特定的 xlink 元素/属性.

XML itself has no requirement at all to declare a namespace which is not used. Therefore you likely can leave it out as well or you only need to add it where you need to add it, e.g. the specific xlink element / attribute later on.

任何支持命名空间的 XML 解析器都将支持任何格式良好的 XML+Namspaces 文档,因此真的没有理由担心根元素是否具有该声明以及带有哪个前缀.Simplexml 只负责这些,只需在需要的地方添加 xlink 属性即可.示例:

Any XML parser that supports namespaces will support any well-formed XML+Namspaces document, so there should really be no reason to worry whether or not the root element has that declaration and with which prefix. Simplexml just takes care of that, just add the xlink attribute where you need it. Example:

$mets = new SimpleXMLElement('<mets></mets>');
$child = $mets->addChild('child');
$child->addAttribute('xlink:href', 'child.xml', 'http://www.w3.org/1999/xlink');
$child = $child->addChild('child');
$child->addAttribute('xlink:href', 'child.xml', 'http://www.w3.org/1999/xlink');
echo $mets->asXML();

输出:

<?xml version="1.0"?>
<mets>
  <child xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="child.xml">
    <child xlink:href="child.xml"/>
  </child>
</mets>

这篇关于在节点中使用 xmlns:xlink 属性创建 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆