如何使用 schemaLocation 或 noNamespaceSchemaLocation 将 XML 链接到 XSD?
Posted
技术标签:
【中文标题】如何使用 schemaLocation 或 noNamespaceSchemaLocation 将 XML 链接到 XSD?【英文标题】:How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation? 【发布时间】:2016-05-26 12:15:50 【问题描述】:我找到了一些解决这个问题的提示,但仍然没有帮助我。
这是我的 XML
<?xml version="1.0" encoding="UTF-8"?>
<work xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.w3.org/2001/XMLSchema-instance"
tns:schemaLocation="myXSDSchema.xsd">
<tns:Objects>
<tns:Object Name=":" Location=":">
</tns:Object>
</tns:Objects>
</work>
这是我的 XSD 文件:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns = "http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
(some checks)
</schema>
我的 XSD 文件与 XML 位于同一文件夹中。
如何链接这两个文件?
【问题讨论】:
【参考方案1】:如何将 XSD 链接到 XML 文档取决于 XML 文档是否使用命名空间...
没有命名空间
使用xsi:noNamespaceSchemaLocation
提供有关要使用的 XSD 的提示:
XML
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="example.xsd">
<!-- ... -->
</root>
XSD
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<!-- ... -->
</xsd:element>
</xsd:schema>
使用命名空间
使用 xsi:schemaLocation
提供有关要使用的 XSD 的提示:
XML
<ns:root xmlns:ns="http://example.com/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com/ns example-ns.xsd">
<!-- ... -->
</ns:root>
XSD
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/ns">
<xsd:element name="root">
<!-- ... -->
</xsd:element>
</xsd:schema>
【讨论】:
没有命名空间,在本地文件系统上指定xsd
的语法是一样的吗?
是的,本地或远程的 XSD 语法相同;两者都是 URL。如果您在指定本地 URL 时遇到问题,请参阅 How to reference a local XML Schema file correctly?以上是关于如何使用 schemaLocation 或 noNamespaceSchemaLocation 将 XML 链接到 XSD?的主要内容,如果未能解决你的问题,请参考以下文章
跨项目指定 XSD schemaLocation 的正确或正确方法是啥?
如何通过使用EclipseLink Moxy的注释来指定XML名称空间,schemaLocation和名称空间URI?
ECSHOP安装或使用中提示Strict Standards: Non-static method cls_image:
使用 FormView 和 ModelForm 时如何在模板上获取 non_field_errors
如何使用 influxdb non_negative_derivative 获得一致的值?
使用Android Camera2,应用无限期陷入STATE_WAITING_PRECAPTURE或STATE_WAITING_NON_PRECAPTURE