xml - 使用 XML 命名空间

标签 xml namespaces

在搜索 Web 时,我遇到了许多在 XML 文件中使用 namespace 的示例。他们大多有这种形式:

<d:student xmlns:d='http://www.develop.com/student'>
  <d:id>3235329</d:id>
  <d:name>Jeff Smith</d:name>
  <d:language>C#</d:language>
  <d:rating>9.5</d:rating>
</d:student>

(这是取自 https://msdn.microsoft.com/en-us/magazine/cc302166.aspx 的示例)

这一行:
<d:student xmlns:d='http://www.develop.com/student'>

困扰我,几乎是因为每个例子都是这样的。它可以有一种形式吗
<student xmlns:d='http://www.develop.com/student'>

所以在这里我声明了由相同 URI 标识的相同命名空间,但我不希望声明具有命名空间的节点。这是正确的吗?长话短说:是 just xmlns:d='http://www.develop.com/student' d: 的有效声明命名空间?

最佳答案

xmlns:d='http://www.develop.com/student'将声明 d该元素及其所有后代的命名空间。

如果缺少 d:,它不会使元素来自该命名空间在标签名称上。那仍将使用默认命名空间。

IE。

<foo xmlns="http://example.com/1">
    <bar xmlns:x="http://example.com/2">
        <x:baz />
    </bar>
</foo>
foo来自 /1 . bar来自 /1 . baz来自 /2 .

关于xml - 使用 XML 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28065186/

相关文章:

php - XML 命名空间 - 如何获取它们?

java - XML 生成后的 JAXB 日期时间格式

c# - 找不到正确的命名空间?

scala - 如何在 Scala 中构建合适的命名空间?

php - 命名空间和全局类的使用

java - 安卓 : Design shown in XML not reflected in the UI

html - 如何修改此 SQL 查询中的 XML 代码以突出显示表的特定行?

c# - 反序列化对象列表的问题

php - 找不到类 'App\Http\Controllers\DB',我也无法使用新模型

linq - 无法解析 System.Linq 命名空间