如何将 XML 链接到 XSD?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/740751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you link XML to a XSD?
提问by cwap
Been wondering, just as we use the -declaration to bind XML to a DTD, how do we do it with XSD?
一直想知道,就像我们使用 -declaration 将 XML 绑定到 DTD 一样,我们如何使用 XSD 来做到这一点?
MSDN sample:
MSDN示例:
<?xml version="1.0"?>
<Product ProductID="123"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Product.xsd">
<ProductName>Rugby jersey</ProductName>
</Product>
is it the xsi:NoNamespaceSchemaLocation that does the trick? Or is this just another namespace?
是 xsi:NoNamespaceSchemaLocation 起作用了吗?或者这只是另一个命名空间?
[EDIT] And is the
[编辑] 是
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
..line just for giving us a unique XML namespace, or does it also provide information on where the schema can be located?
..line 只是为了给我们一个唯一的 XML 命名空间,还是它也提供了关于模式可以位于何处的信息?
采纳答案by Peter
Try schemaLocation.
尝试 schemaLocation。
<?xml version="1.0"?>
<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
noNamespaceSchemaLocation is different. Note that both are indeed only "hints" in theory, to a consumer of an XML document. I've never run into an xml processor that did not follow them; after all, it's a W3C recommendation. see http://www.w3.org/TR/xmlschema-1/
noNamespaceSchemaLocation 是不同的。请注意,对于 XML 文档的使用者而言,两者实际上只是理论上的“提示”。我从来没有遇到过不遵循它们的 xml 处理器;毕竟,这是 W3C 的建议。见http://www.w3.org/TR/xmlschema-1/
But indeed, it couldgo wrong, as here, but then again, it's considered a bug for a reason.
但确实,它可能会出错,就像这里,但话又说回来,出于某种原因,它被认为是一个错误。
To go short : I just trust it, with no harm so far :-)
简而言之:我只是相信它,到目前为止没有任何伤害:-)
I don't think any half-decent xml processor can ignore this 'hint' these days.
我认为这些天没有任何半体面的 xml 处理器可以忽略这个“提示”。
The urls are always for uniqueness, but in some cases some info will be provided under the URL.
网址始终是唯一的,但在某些情况下,网址下会提供一些信息。
回答by John Saunders
xsi:noNamespaceSchemaLocation and xsi:schemaLocation both provide hints to XML processors who decide to obey those hints. But they are only hints. They do not necessarily cause your document to validate against the schema.
xsi:noNamespaceSchemaLocation 和 xsi:schemaLocation 都向决定遵守这些提示的 XML 处理器提供提示。但它们只是提示。它们不一定会导致您的文档根据架构进行验证。
回答by D.Shawley
I generally just include the namespace and expect that if whomever is processing it cares to validate it, then they will get the schema and set up their processing environment so that it can find the XSD. I've had limited success with xsi:schemaLocationand such attributes. Most of the problems are usually centered around finding the XSD file itself. Some processors want the path to be included which is loads of fun if the XSD is on a file system instead of a web server.
我通常只包含命名空间,并希望如果处理它的人关心验证它,那么他们将获得架构并设置他们的处理环境,以便它可以找到 XSD。我在xsi:schemaLocation和这些属性方面取得的成功有限。大多数问题通常都集中在查找 XSD 文件本身。一些处理器希望包含路径,如果 XSD 位于文件系统而不是 Web 服务器上,这会很有趣。
Every processor seems to implement the lookup a little differently. Some use separate schema catalog objects, others require you to load and attach schemas separately. Unless you are providing the code to process documents, you are best off to not include xsi:schemaLocationor xsi:noNamespaceSchemaLocationIMHO. The only thing that their inclusion can do is hamstring whomever is processing your document into either placing the schema in the same location or find some way to make their chosen processor ignore or workaround the location specification.
每个处理器似乎都以不同的方式实现查找。有些使用单独的模式目录对象,有些则需要您单独加载和附加模式。除非您提供处理文档的代码,否则最好不要包含xsi:schemaLocation或xsi:noNamespaceSchemaLocation恕我直言。它们的包含唯一能做的就是限制正在处理您的文档的任何人,要么将架构放置在同一位置,要么找到某种方法使他们选择的处理器忽略或解决位置规范。
As a side note, the biggest problem that I've run into was actually with DTDs that were specified using a SYSTEMdeclaration that referred to "c:\somepath\doc.dtd". The problem was that I was processing the documents on a FreeBSD box. I ended up writing my own resolver to map Windows-style paths to a local file system since I could not modify the documents themselves and was required to validate them.
作为旁注,我遇到的最大问题实际上是使用SYSTEM引用"c:\somepath\doc.dtd". 问题是我在 FreeBSD 机器上处理文档。我最终编写了自己的解析器来将 Windows 样式的路径映射到本地文件系统,因为我无法自己修改文档并且需要验证它们。
回答by McDowell
It is not a stupid question, but John Saunders has it right.
这不是一个愚蠢的问题,但约翰桑德斯说得对。
just as we use the -decleration to bind XML to a DTD, how do we do it with XSD?
就像我们使用 -decleration 将 XML 绑定到 DTD 一样,我们如何使用 XSD 来做到这一点?
Here is the essence of the problem - you can't. One of the problems with the DTD approach was that the document specified the validation mechanism and not the document consumer. Post-DTD, you can take an XML document and validate it with XSD or RELAX NG or some other mechanism - they are decoupled (in theory at least). Any XSD link is only a hint and is optional. It is not possible to validate an arbitrary document.
这就是问题的本质——你不能。DTD 方法的问题之一是文档指定了验证机制,而不是文档使用者。在 DTD 之后,您可以获取 XML 文档并使用 XSD 或 RELAX NG 或其他一些机制对其进行验证 - 它们是解耦的(至少在理论上)。任何 XSD 链接都只是一个提示,是可选的。无法验证任意文档。

