如何在 XML 文档中使用本地机器上的架构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4856455/
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 to use Schema that is on local machine in XML document
提问by grobartn
I have limited knowledge of XML/Schema files.
我对 XML/Schema 文件的了解有限。
So this should be a fairly simple question. How do you specify a local file for the 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">
...
</note>
This is a sample from www.w3.org and the part that specifies schema is in schemaLocation. I tried looking at the documentation, but how can you specify a local file?
这是来自 www.w3.org 的示例,指定架构的部分位于 schemaLocation。我尝试查看文档,但是如何指定本地文件?
Something like
就像是
xsi:schemaLocation="../relativepath/schemafolder not.xsd">
Thanks
谢谢
回答by biziclop
schemaLocationhas to contain two values separated by whitespace: the namespace URI (this doesn't change) and the schema url.
schemaLocation必须包含两个由空格分隔的值:命名空间 URI(这不会改变)和架构 url。
So in your case
所以在你的情况下
xsi:schemaLocation="http://www.w3schools.com ../relativepath/schemafolder/note.xsd">
Don't be fooled by the namespace URI being a seemingly valid http url, that's just one of the little madnesses XML people invented. :)
不要被命名空间 URI 是一个看似有效的 http url 所迷惑,这只是 XML 人发明的小疯狂之一。:)
Actually, you can specify schemas for several namespaces in one schemaLocation:
实际上,您可以在一个中为多个命名空间指定架构schemaLocation:
xsi:schemaLocation="namespace1 schemaurl1 namespace2 schemaurl2 ..."
(I also advise you to use relative paths with care: while they are extremely useful when you move your files around but still validate it with the same code (or tool), when you deploy your validation code in an application, the "working directory" might not be what you expected. That is not to say you shouldn't use relative paths, just to be aware of this when you get a weird looking exception about the schema not found.)
(我还建议您谨慎使用相对路径:虽然它们在您四处移动文件但仍使用相同的代码(或工具)对其进行验证时非常有用,但当您在应用程序中部署验证代码时,“工作目录” " 可能不是您所期望的。这并不是说您不应该使用相对路径,只是在您遇到有关未找到架构的奇怪异常时要注意这一点。)

