How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?

自作多情 提交于 2019-11-26 01:08:52

问题


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

Here is my 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>

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>

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

How to link these 2 files?


回答1:


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

Without namespaces

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>
    

With namespaces

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>
    


来源:https://stackoverflow.com/questions/35411871/how-to-link-xml-to-xsd-using-schemalocation-or-nonamespaceschemalocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!