开发者

XML validation error when using multiple schema files/namespaces

开发者 https://www.devze.com 2022-12-23 18:44 出处:网络
I\'ve been reading a ton about xml and learning a lot but I am stuck on one error. I have a schema defined in multiple files and I can\'t get it to work.Here is an example

I've been reading a ton about xml and learning a lot but I am stuck on one error.

I have a schema defined in multiple files and I can't get it to work. Here is an example

==================================

libraryBooks.xsd

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns="urn:MyNamespace"
  targetNamespace="urn:MyNamespace"
  elementFormDefault="qualified"
  >

  <xsd:element name="libraryBooks" type="libraryBooksType"/>

  <xsd:complexType name="libraryBooksType">
    <xsd:sequence>
       <xsd:any minOccurs="0"/>
    </xsd:sequence>
    <xsd:attribute name="name" type="xsd:string"/>
  </xsd:complexType>
</xsd:schema>

==================================

book.xsd

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns="urn:MyNamespace2"
  targetNamespace="urn:MyNamespace2"
  elementFormDefault="qualified"
  >

  <xsd:element name="book" type="booksType"/>

  <xsd:complexType name="bookType">
    <xsd:attribute name="title" type="xsd:string"/>
  </xsd:complexType>
</x开发者_运维百科sd:schema>

==================================

myXml.xml

<?xml version="1.0" encoding="utf-8" ?>
  <libraryBooks
     xmlns="urn:MyNamespace"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="urn:MyNamespace file:///C:/libraryBooks.xsd"

     name="CentralLibrary">
     <mn2:book 
        xmlns:mn2="file:///C:/book.xsd"
        title="How to make xml work the way I want">
     </mn2:book>
  </libraryBooks>

So the error I get would be "The 'file:///C:/book.xsd:book' element is not found". Any ideas? I'm almost certain it is something simple

Edit: Thank you so much everyone. I was close but just stuck on one minor thing. Really appreciate the direction. I tried both methods and in case anyone else reading this is wondering, the way to enter multiple schemaLocations at the root node is

xsi:schemaLocation="urn:MyNamespace file:///C:/libraryBooks.xsd
                    urn:MyNamespace2 file:///C:/book.xsd"


You got it right for the first namespace, but for the second one, you somehow got the namespace URI and the schema location mixed up. Try this:

 <mn2:book 
    xmlns:mn2="urn:MyNamespace2"
    xsi:schemaLocation="urn:MyNamespace2 file:///C:/book.xsd"
    title="How to make xml work the way I want">
 </mn2:book>

... you can additionally move the xmlns:mn2 and the xsi:schemaLocation up to the top level (similarly to what Grzenio suggested), if you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号