如何在 XML 文档中使用本地机器上的架构 [英] How to use Schema that is on local machine in XML document

查看:32
本文介绍了如何在 XML 文档中使用本地机器上的架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 XML/Schema 文件的了解有限.

I have limited knowledge of XML/Schema files.

所以这应该是一个相当简单的问题.如何为 schemaLocation 指定本地文件?

So this should be a fairly simple question. How do you specify a local file for the 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>

这是来自 www.w3.org 的示例,指定架构的部分位于 schemaLocation.我尝试查看文档,但如何指定本地文件?

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?

类似的东西

xsi:schemaLocation="../relativepath/schemafolder not.xsd">

谢谢

推荐答案

schemaLocation 必须包含两个由空格分隔的值:命名空间 URI(这不会改变)和架构 url.

schemaLocation has to contain two values separated by whitespace: the namespace URI (this doesn't change) and the schema url.

所以你的情况

 xsi:schemaLocation="http://www.w3schools.com ../relativepath/schemafolder/note.xsd">

不要被命名空间 URI 作为一个看似有效的 http url 所迷惑,这只是 XML 人发明的小疯狂之一.:)

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. :)

实际上,您可以在一个 schemaLocation 中为多个命名空间指定架构:

Actually, you can specify schemas for several namespaces in one 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.)

这篇关于如何在 XML 文档中使用本地机器上的架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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