【问题标题】:How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?如何使用 schemaLocation 或 noNamespaceSchemaLocation 将 XML 链接到 XSD?
【发布时间】: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 位于同一文件夹中。

如何链接这两个文件?

【问题讨论】:

    标签: xml xsd xsd-validation xml-validation


    【解决方案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?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    相关资源
    最近更新 更多