使用 xmlns 作为普通 XML 属性?

Using xmlns as a normal XML attribute?

我有以下 XML 正在使用 pugi xml 在 C++ 中解析:

<root>

<table xmlns="http://www.w3.org/TR/html4/">
<tr>
  <td>Apples</td>
  <td>Bananas</td>
</tr>
</table>

<table xmlns="http://www.w3schools.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>

</root>

为了仅提取属于 http://www.w3.org/TR/html4/ 命名空间的 table 元素,我想我只是将 xmlns 视为常规 XML 属性,并且试试下面的 XPath:/root/table[@xmlns='http://www.w3.org/TR/html4/']。但是,这样做 returns 0 行。

当我向 xmlns='http://www.w3.org/TR/html4/ 添加前缀(例如 xmlns:htmlns)时,我能够将命名空间作为属性检索,因此我似乎可以处理 xmlns 属性在一定程度上作为常规属性。如果有命名空间前缀,我可以只使用 xmlns 作为普通属性吗?其使用规则是什么?

不,xmlns 不能作为常规属性使用,但有一个 xpath 函数,试试

/root/*[local-name() = 'table' and namespace-uri()='http://www.w3.org/TR/html4/']

您要么需要删除前缀,要么在 xpath 中使用该前缀,例如 ...

xmlns=''
xmlns:html='http://www.w3.org/TR/html4' // I don't know how to do this with your library

/root/html:table

...或...

您需要 select 所有子元素,然后过滤名称空间,例如

/root/*[namespace-uri()='http://www.w3.org/TR/html4/']