Domanda

I have an XML schema document (provided by a company I deal with) to validate a certain XML file that is required for their system.

Lets say the first couple of lines of the schema look like this (I have modified the URIs to protect the guilty!):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.example.com/manifest-plugin"
xmlns="http://www.example.com/manifest-plugin">

The URI in the XSD is not valid in the unmodified original i.e. you cannot go there to get a copy of this XSD file. I have no idea why they haven't published it at the URL defined in the XSD, but they haven't.

I have a local copy of the XSD which I would like to use to validate the XML file I create using the features of Eclipse's XML catalog. Obviously, as the URI is invalid there is no point to let the system try to retrieve the XSD from the web.

Let's say the local XSD file lives on my local drive here:

C:\xml_schemas\manifest-plugin.xsd

And the opening two lines of the XML file look like this:

<?xml version="1.0" encoding="UTF-8"?>
<manifest>

Could anyone give an example of how to do this?

Specifically:

  1. What values do I set in the XML catalog dialog boxes in Eclipse, based on the values I have provided above?
  2. What do I put into the XML file to get it to validate against the local XSD, without confusing other systems which my read my XML file down the line?

I have found this page on the Eclipse wiki, but I personally don't find the instructions very clear:-

Using the XML catalog in Eclipse

I have tried setting the XML catalog parameters thus:

Then adding

<manifest xmlns:targetNamespace="http://www.example.com/manifest-plugin">

to the XML file, but it doesn't seem to work.

By the way, if your version of Eclipse doesn't have the XML catalog, you probably haven't got the "web tools platform (WTP)" features installed. They come with the Java EE version of Eclipse, but can be installed separately.

È stato utile?

Soluzione

If I'm not mistaken, you can specify a local schema location which will take precedence over the remote location of the XML Schema file.

As such, if you put your XSD file in the directory where your XML files are, the header of your manifest.xml file should be:

<manifest xmlns="http://my.super.xsd.com" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3schools.com manifest.xsd">

I tested under Eclipse, it works (you can even click on the XSD link). Now, that works if both your XSD and XML files are in the same directory.

If you want to have your XSD in a separate local location, you have to use the file protocol. Given your example, your XML file would look like so:

<manifest xmlns="http://my.super.xsd.com" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3schools.com file:///C|/xml_schemas/manifest-plugin.xsd"">

This example also works under Eclipse, although when clicking on the XSD, it opens the web browser (at least for me).

Altri suggerimenti

It should be pretty straight forward. From the XML Catalog preferences page click "Add" to get the "Add XML Catalog Element" dialog. There you want to add a Catalog Entry with the following attributes:

The local XSD can either be in your workspace (using Eclipse path variables) or on the file system external to the workspace. After adding the entry Eclipse should be able to validate XML documents in the workspace.

Note that the XML Catalog doesn't have anything to do with your applications runtime validation of XML documents.

Update: The XML file should then contain the following:

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns="http://www.example.com/manifest-plugin">
...

In Eclipse 3.6 or 3.7 you should see a dialog like this: enter image description here

if schemaloaction is not specified in the xml, you should be able to use XML catelog in Eclipse to override the schema location.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top