如何使用schemaLocation或noNamespaceSchemaLocation将XML链接到XSD?

por*_*ddr 5 xml xsd xml-validation xsd-validation

我找到了一些关于这个问题的提示,但仍然没有帮助我.

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的XSD文件:

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

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

如何链接这2个文件?

kjh*_*hes 11

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

没有名称空间

使用xsi:noNamespaceSchemaLocation提供一个提示,到XSD使用:

使用名称空间

使用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>
    
    Run Code Online (Sandbox Code Playgroud)
  • XSD

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://example.com/ns">
      <xsd:element name="root">
        <!-- ... -->
      </xsd:element>
    </xsd:schema>
    
    Run Code Online (Sandbox Code Playgroud)

  • 是的,XSD 语法对于本地或远程是相同的;两者都是 URL。如果您在指定本地 URL 时遇到问题,请参阅[如何正确引用本地 XML 架构文件?](/sf/ask/1347738171/) (2认同)