Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference local XSD from XML file?

I want to write a simple XML file with a custom xsd file located in the same local directory on my computer. I don't understand the necessary syntax at the beginning of the files (I have googled but XSD tutorials seem to focus on the element definitions rather than the xsd:schema).

My sys_params.xsd begins:

<xsd:schema xmlns:xsd="http://www.w3.org/1999/XMLSchema">

<xsd:element     name="shipOrder" type="order"/>

My sys_params.xml begins:

<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com sys_params.xsd"

<orderperson>John Smith</orderperson>

My XML reports this validation error on the xml:

Error schema document 'sys_params.xsd' has different target namespace from the one specified in instance document 'http://www.w3schools.com'

No doubt my code is quite wrong but I need some help to correct it please.

like image 690
DavidA Avatar asked Dec 24 '22 03:12

DavidA


1 Answers

Change

xsi:schemaLocation="http://www.w3schools.com sys_params.xsd"

to

xsi:noNamespaceSchemaLocation="sys_params.xsd"

because your XML is not in a namespace.

See also:

  • How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation? to understand how to reference an XSD from an XML file, depending on whether namespaces are being used or not.
  • How to reference a local XML Schema file correctly? if you have trouble stating the specification of the local XSD file. [Note, however, that despite the title similarities, the other link I provide is actually what will help you most with your question.]
like image 119
kjhughes Avatar answered Dec 28 '22 07:12

kjhughes