Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct or proper way to specify XSD schemaLocation across projects?

Tags:

java

xsd

Say I have two projects, A and B. Java projects, in case that's important.

Project A contains a bunch of XSD files that represent core types and elements. They are all placed in a package called, say, "definition". This gets built into project-a.jar.

Project B represents an extension project, and it's allowed to defined its own types and elements. I created a new schema and placed it in "definition.extension" package. This gets built into project-b.jar.

Now, for the XSDs in Project B, what exactly should I put as the schemaLocation for an include?

schemaLocation="../core-types.xsd" didn't quite work (I know, it's needs a URI), but what exactly is the proper or standard approach to this? Google found me more people asking this question that clear-cut, standard approaches on what really is the correct way to handle this.

It can't be that I have programmatically adjust the schemaLocation during runtime... or that I'd need a build step/script that will dynamically replaced the schemaLocation during compilation... right?

I'm not looking for answers like "put them in a shared location". I'm looking for something more along the lines of a dev environment that uses relative references instead of hardcoded references.

FYI, I'm using IntelliJ IDEA, in case there's an IDE-specific approach.

like image 778
aberrant80 Avatar asked Mar 13 '13 07:03

aberrant80


People also ask

What is schemaLocation in XSD?

The xsi:schemaLocation attribute locates schemas for elements and attributes that are in a specified namespace. Its value is a namespace URI followed by a relative or absolute URL where the schema for that namespace can be found. It is most commonly attached to the root element but can appear further down the tree.

How do you reference an XML schema?

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 you add multiple namespaces in XSD?

You have to use two schemas. one schema per namespace. you have to use xsd:import to bring in an XSD from a different namespace. You have to Validate the xml document using only the main schema (mc.

What is the use of targetNamespace in XSD?

Figure 1: Elements and attributes in XML Schema namespace are used to write an XML Schema document, which generates elements and attributes as defined by user and puts them in {target namespace}. This {target namespace} is then used to validate the XML instance.


1 Answers

If you just want IntelliJ to stop showing your includes in red, you can use some custom URI in your include. You then go to Project Settings -> Schema's and DTD's where you can map this URI onto a local file.

If you need to do schema validation at run time, that's a different story. You probably need to use an XML Catalog. If you're using JAXB, you should have a look at this question: jaxb - how to map xsd files to URL to find them

like image 175
Pieter Herroelen Avatar answered Nov 14 '22 22:11

Pieter Herroelen