配置 XML 命名空间 [英] Configuring an XML Namespace

查看:34
本文介绍了配置 XML 命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个带有几个不同选项的 XML 生成器,其中一个是样式选项,它定义是对原始数据类型使用属性还是元素.

I have made an XML generator with a few different options, one is a style option which defines whether to use attributes or elements for primitive data types.

目前不支持 XML 模式,但我需要允许设置 XML 命名空间,并且我一直在做一些研究.我的理解是 XML 命名空间可以有前缀,但不是必须的.它还需要一个唯一的字符串属性值,该值通常是一个 URI,但不一定是.

XML schemas aren't supported right now, but I need to allow the setup of an XML Namespace, and I have been doing some research. My understanding is the XML namespace can have a prefix, but it doesn't have to. It also needs a unique string attribute value that is usually a URI, but doesn't have to be.

我有点困惑,因为我是 XML 命名空间的新手,如果我们看一下示例 xml 文档,我对此有一些疑问

I am a little bit confused, as I am new to XML namespaces, and I have a few questions about this, if we take a look at an example xml document

<?xml version="1.0"?>
<root xmlns="some_identifier">
<oneKey>value</oneKey>
</root>

这是使用键值示例的元素样式,我将允许配置some_identifier".在这个例子中是 XML 命名空间下的oneKey"元素吗?或者我是否必须指定像 xmlns:ns 这样的前缀,然后用ns"前缀oneKey"?

This is using an element style of the key value example, and I'm going to allow the configuration of "some_identifier". In this example is the "oneKey" element under the XML namespace? Or do I have to specify a prefix like xmlns:ns and then prefix "oneKey" with "ns"?

另外,如果我们看一下属性样式:

Also if we take a look at attribute style:

<?xml version="1.0"?>
<root xmlns="some_identifier" oneKey="value" />

在这个例子中,我们需要在 oneKey 中定义一个类似的前缀吗?

Do we need to define a similar prefix in oneKey in this example?

如果我在这些问题上跑题了,我深表歉意,如果我没有说清楚,请告诉我,

I apologize if I'm way off the mark in these questions, please let me know if I'm just not making sense,

更新:

我找到了这个网站:http://www.rpbourret.com/xml/命名空间FAQ.htm#exec_1

也就是说这两个是相同的:

That says these two are identical:

<foo:A xmlns:foo="http://www.foo.org/">
     <foo:B>abcd</foo:B>
</foo:A>

<A xmlns="http://www.foo.org/">
     <B>abcd</B>
</A>

这是非常有用的,但是关于属性样式.我需要为属性添加前缀吗?或者 xmlns 默认也适用于这些?

which is very useful, but regarding the attribute style. Do I need to prefix attributes? Or will the xmlns default work for these as well?

推荐答案

<root xmlns="some_identifier">

这声明了默认命名空间,root 及其所有子项都将属于它,您不需要前缀.

this declares default namespace, root and all its children will belong to it, you don't need prefixes.

<root xmlns="some_identifier" oneKey="value" />

默认命名空间不适用于属性.如果你想让 oneKey 属于一个命名空间,你必须创建一个前缀并在属性上使用它

Default namespaces do not apply to attributes. If you want oneKey to belong to a namespace, you have to create a prefix and use it on the attribute

<root xmlns="some_identifier" xmlns:myns="some_identifier" myns:oneKey="value"/>

这篇关于配置 XML 命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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