XML 命名空间和 schemalocation 属性?

发布于 2024-11-17 04:11:19 字数 945 浏览 1 评论 0原文

在许多 xml 示例中,我看到许多名称模式甚至不在 xml 中使用,如下例所示,我们将名称模式定义为 xsiaop 和一个默认值 命名空间。如果我们不使用它们,定义它们的目的是什么。没有附加 xml,因为它很大。

第二个问题是:- 假设我们使用这个名称架构。我们通常使用一些网址定义名称架构和 schemalocation 的值 就像http://someAddress //。如果我们用一些任意值(例如 ABC 或其他值)定义它们会怎么样?会有什么不同吗?有没有 我们对这些网址进行的处理?

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/ 
schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema 
/aop/spring-aop-2.0.xsd"> 

另一个问题是我们只为 xsi 命名空间定义了 schemalocation ,而不是为 aop 命名空间。为什么会这样呢?

最后一个问题是我们可以定义命名空间xsi1而不是xsi吗?它是否使任何我

In many xml examples i see many nameschemas which even dont use in xml like in below example we have defined nameschemas as xsi, aop and one default
namespace. If we dont use them what the purpose of defining them. Did not attach the xml as its quite big.

Second question is :- Assume that we using this nameschemas.. We usually defined the values of namechemas and schemalocation with some web address
like http://someAdddress//. What if we define them with some arbitrary value like ABC or something else. Will it make any differnce? Is there any
processing we do from these web addresses?

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/ 
schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema 
/aop/spring-aop-2.0.xsd"> 

Another question is we have defined the schemalocation for xsi namespace only Not for aop namespace. Why so?

Last question is can we define namespace xsi1 instead of xsi? Does it make any im

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆伤 2024-11-24 04:11:19

一般来说,如果文档中未使用名称空间绑定,则该名称空间的声明是多余的,可以安全地省略(相反,它不会因存在而造成任何损害。)我说它应该情况是这样,但也有例外。一种是 DTD 验证,有时需要特定元素上是否存在特定命名空间声明(DTD 处理不识别命名空间,因此它将命名空间视为属性)。另一个相当罕见的例外:我见过 XML 词汇表,其中命名空间声明用于传达语义,而不是简单地声明命名空间前缀的绑定,例如它可能指示版本信息。这是糟糕的 XML 设计,而且还有很多糟糕的 XML 设计。

至于 xsi:schemaLocation,一些工具/应用程序会忽略它,其他工具/应用程序可能会将其解释为针对在特定位置找到的模式执行模式验证的请求。

Generally it should be the case that if a namespace binding isn't used within a document, a declaration of that namespace is redundant and can safely be omitted (conversely, it won't do any harm by being there.) I say it should be the case, but there are exceptions. One is DTD validation, which sometimes requires the presence or absence of a particular namespace declaration on a particular element (DTD processing isn't namespace aware, so it treats namespaces as attributes). Another rather rare exception: I have seen XML vocabularies where namespace declarations are used to convey semantics other than simply declaring a binding for a namespace prefix, for example it might indicate version information. That's bad XML design, but there's a lot of bad XML design about.

As for xsi:schemaLocation, some tools/applications will ignore this, others may interpret it as a request to perform schema validation against a schema found at a particular location.

很酷又爱笑 2024-11-24 04:11:19

在第一段中,您问如果我们定义 XML 命名空间但不使用它怎么办?

好吧,不会有什么不好的事情发生。它只是供你使用,而你并不使用它。它就像在函数中定义的变量,但您只是不使用。

现在,您的其中一项声明存在错误,即您没有使用 xsi、aop 和默认名称空间。那不是真的。因为xsi是xml模式实例。您可以在下一行编写 时使用它,它基本上可以让您创建在 xsd/dtd 文件中创建的架构定义的实例。你可能/可能没有使用过aop,在你给我看xml 文件之前我无法判断。现在有了默认名称空间,您会有点困惑。 XML 文档中的默认命名空间告诉模式验证器此 XML 文档中使用的所有元素都在“”命名空间中声明。请参阅标题下的 http://www.w3schools.com/Schema/schema_schema.asp “在 XML 文档中引用架构”。

现在你的第二个问题是所有命名空间通常都定义为 url。我们可以将其定义为其他名称吗?

是的,你可以。这是一个示例 xsd 和 xml 文件。

<?xml
    version="1.0"
    encoding="UTF-8"
?>

<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="mynamespace"
    xmlns="mynamespace"
    elementFormDefault="qualified"
>
    <xs:element name="note">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="to">
                    <xs:complexType>
                        <xs:attribute name="lang" type="xs:string" use="required"></xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

这是 XML:

<?xml
    version="1.0"
    encoding="utf-8"
?>

<note
    xmlns:note="mynamespace"
    xmlns="mynamespace"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="mynamespace myschema.xsd"
>
    <note:to lang="en"></note:to>
</note>

所以您可以看到我为命名空间定义了另一个名称,并且它在我的 Eclipse 中完美运行。

我认为(我在这里猜测)人们使用 URI 作为命名空间是有原因的。因为如果您只是像我一样保留一个名称“mynamespace”,而其他人想要导入/使用您的架构,那么您的名称和他们的名称可能会发生冲突。现在你不能写一个简单的名称并希望世界上的每个人都不会使用该名称,因此人们使用 URI 作为他们的名称空间,因为 URI 是唯一的,其他人没有理由在为其应用程序创建架构时使用你的 URI 。

您的第三个问题是为什么我们没有像为 xsi 那样为应用程序定义 schemaLocation?再次查看您的 schemaLocation。您已定义 4 个“空格分隔”值。 2 个用于 xsi,两个用于 aop。

您的最后一个问题是您可以定义 xsi1 而不是 xsi。你可以。事实上,您可以定义任何您想要的名称。它是一个变量,用作表示名称空间的简写。

PS:我最近开始学习XML/XSD。因此,如果您发现我的答案错误,请分享正确的答案,我将编辑我的答案。谢谢。

In your first paragraph you asked what if we define an XML namespace and don't use it?

Well nothing bad will happen. It's just there for your use and you don't use it. Its like a variable defined in your function that you just don't use.

Now there is an error in one of your statement that you do not use xsi, aop and default namespace. That is not true. Because xsi is xml schema instance. You use it in the next line where you write <xsi:schemaLocation ... /> It basically lets you create an instance of the schema definition that you have created in your xsd/dtd file. You may/may not have used aop, I cannot tell until you show me the xml file. Now with default namespace, you are a little bit confused. Default namespace in XML documents tells the schema-validator that all the elements used in this XML document are declared in the "<some namespace>" namespace. See http://www.w3schools.com/Schema/schema_schema.asp under the heading "Referencing a Schema in an XML Document".

Now your second question is that all the namespaces are generally defined as a url. Can we define it as some other name ?

Yes, you can. Here is a sample xsd and xml file.

<?xml
    version="1.0"
    encoding="UTF-8"
?>

<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="mynamespace"
    xmlns="mynamespace"
    elementFormDefault="qualified"
>
    <xs:element name="note">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="to">
                    <xs:complexType>
                        <xs:attribute name="lang" type="xs:string" use="required"></xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Here is the XML:

<?xml
    version="1.0"
    encoding="utf-8"
?>

<note
    xmlns:note="mynamespace"
    xmlns="mynamespace"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="mynamespace myschema.xsd"
>
    <note:to lang="en"></note:to>
</note>

So you can see that I have defined another name for the namespace and it works perfectly in my eclipse.

I think (and I am guessing here) that there is a reason why people use URI as the namespace. Because if you simply keep a name like I did "mynamespace" and other people want to import/use your schema, then your name and their name might collide. Now you cannot write a simple name and hope that every person in the world will not use that name, so people use URI as their namespaces, because URI are unique and there is no reason for others to use your URI while creating schemas for their applications.

Your 3rd question was why have we not defined a schemaLocation for app just like we did for xsi? Look again in your schemaLocation. You have defined 4 "space separated" values. 2 for xsi and two for aop.

Your last question was can you define xsi1 instead of xsi. You can. Infact you can define any name you want. Its a variable that is used as a shorthand to represent the namespace.

PS: I have started learning XML/XSD recently. So if you find my answer wrong, please share the correct answer and I will edit mine. Thank You.

蓝色星空 2024-11-24 04:11:19

如果您想在不使用命名空间的情况下将 XSD 架构分配给 XML 文件,也可以简单地使用 xsi:noNamespaceSchemaLocation 属性而不是 xsi:SchemaLocation来源

示例:

<myElement
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="myschema.xsd"
>

If you want to assign a XSD Schema to a XML file without using a namespace, you can also simply use the xsi:noNamespaceSchemaLocation attribute instead of xsi:SchemaLocation. Source

Example:

<myElement
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="myschema.xsd"
>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文