Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Schema that is on local machine in XML document

Tags:

I have limited knowledge of XML/Schema files.

So this should be a fairly simple question. How do you specify a local file for the schemaLocation?

<?xml version="1.0"?>  <note   xmlns="http://www.w3schools.com"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://www.w3schools.com note.xsd">   ... </note> 

This is a sample from www.w3.org and the part that specifies schema is in schemaLocation. I tried looking at the documentation, but how can you specify a local file?

Something like

xsi:schemaLocation="../relativepath/schemafolder not.xsd"> 

Thanks

like image 539
grobartn Avatar asked Jan 31 '11 21:01

grobartn


People also ask

How do you reference a schema in XML?

Reference the XSD schema in the XML document using XML schema instance attributes such as either xsi:schemaLocation or xsi:noNamespaceSchemaLocation. Add the XSD schema file to a schema cache and then connect that cache to the DOM document or SAX reader, prior to loading or parsing the XML document.

How do I find the XML Schema?

With the desired XML document opened in the active editor tab, choose Tools | XML Actions | Generate XSD Schema from XML File on the main menu. The Generate Schema From Instance Document dialog box opens. and select the desired file in the dialog that opens.

What is schema instance in XML?

The schema instance namespace (http://www.w3.org/2001/XMLSchema-instance) defines a few attributes that are used in instance documents with special meaning. The xsi:noNemaspaceSchemaLocation and xsi:schemaLocation attributes are used to associate XML Schemas with XML documents.


1 Answers

schemaLocation has to contain two values separated by whitespace: the namespace URI (this doesn't change) and the schema url.

So in your case

 xsi:schemaLocation="http://www.w3schools.com ../relativepath/schemafolder/note.xsd"> 

Don't be fooled by the namespace URI being a seemingly valid http url, that's just one of the little madnesses XML people invented. :)

Actually, you can specify schemas for several namespaces in one schemaLocation:

xsi:schemaLocation="namespace1 schemaurl1 namespace2 schemaurl2 ..." 

(I also advise you to use relative paths with care: while they are extremely useful when you move your files around but still validate it with the same code (or tool), when you deploy your validation code in an application, the "working directory" might not be what you expected. That is not to say you shouldn't use relative paths, just to be aware of this when you get a weird looking exception about the schema not found.)

like image 90
biziclop Avatar answered Sep 26 '22 01:09

biziclop