如何使用 schemaLocation 或 noNamespaceSchemaLocation 将 XML 链接到 XSD? [英] How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?

查看:28
本文介绍了如何使用 schemaLocation 或 noNamespaceSchemaLocation 将 XML 链接到 XSD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了解决这个问题的一些技巧,但仍然没有帮助我.

I found some tips for this problem, but still didn't help me.

这是我的 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 文件:

Here is my XSD file:

<schema xmlns="http://www.w3.org/2001/XMLSchema" 
        xmlns:tns = "http://www.w3.org/2001/XMLSchema" 
        elementFormDefault="qualified">
  (some checks)
</schema>

我的 XSD 文件与 XML 位于同一文件夹中.

My XSD file is located in the same folder as the XML.

如何链接这两个文件?

推荐答案

如何将 XSD 链接到 XML 文档取决于 XML 文档是否使用命名空间...

How to link an XSD to an XML document depends upon whether the XML document is using namespaces or not...

使用 xsi:noNamespaceSchemaLocation 提供有关要使用的 XSD 的提示:

Use xsi:noNamespaceSchemaLocation to provide a hint as to the XSD to be used:

  • 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 的提示:

    Use xsi:schemaLocation to provide a hint as to the XSD to be used:

    • 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>
    

  • 这篇关于如何使用 schemaLocation 或 noNamespaceSchemaLocation 将 XML 链接到 XSD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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