9 Jan 2006

XSD schema validation for XSL (and auto-completion in eclipse)

Update: I you are interested only by the eclipse part of this post take a look at a more recent/specialize post: "XSD schema validation for XSL and auto-completion in eclipse (second edition)"

If you want information about xerces, ant, xsl and validation keep reading this one ;) Good luck!



I wanted to have auto-completion and validation on xsl in eclipse. I did it but it was hard!

First You need to understand how xsd works. For syntax examples see xerces doc at the bottom of the page.
You can add those lines to your stylesheet:

<xsl:stylesheet xsl="http://www.w3.org/1999/XSL/Transform" xsi="http://www.w3.org/2001/XMLSchema-instance" schemalocation="http://www.w3.org/1999/XSL/Transform http://www.w3.org/2005/02/schema-for-xslt20.xsd" version="2.0">

But according to the w3c spec you can override the schema location with a global attribut.
The W3C doc about this: http://www.w3.org/TR/xmlschema-1/#xsi_schemaLocation
"2.6.3 xsi:schemaLocation, xsi:noNamespaceSchemaLocation
The xsi:schemaLocation and xsi:noNamespaceSchemaLocation attributes can be used
in a document to provide hints as to the physical location of schema documents
which may be used for ·assessment·. See How schema definitions are located on
the Web (§4.3.2) for details on the use of these attributes."
In eclipse-3.1 you can do it in Window>Preferences>Web and XML>XML Catalogue:

In Xerces you can do it with a property - see the doc.

In Ant you can set xerces property in xmlvalidate ant task (but there was a bug, I provided a patch here)
An ant build file snippet to validate xsl:

<xmlvalidate failonerror="yes" lenient="no" warn="yes">
<attribute name="http://xml.org/sax/features/validation" value="true">
<attribute name="http://apache.org/xml/features/validation/schema" value="true">
<attribute name="http://xml.org/sax/features/namespaces" value="true">

<property name="http://apache.org/xml/properties/schema/external-schemaLocation" value="http://www.w3.org/1999/XSL/Transform http://www.w3.org/2005/02/schema-for-xslt20.xsd">

<fileset dir="xsl-schemaLocation" includes="*.xsl">
</fileset>
</property></attribute></attribute></attribute></xmlvalidate>

Interesting resources I found will investigating all this:

Technorati tags:

4 comments:

Anonymous said...

Thanks a lot - very useful, was searching the "XML Catalog" function of Eclipse

Kevin Kilroy said...

Hi,

I am too trying to get content assist (ctrl + space) to work with xsl stylesheets in eclipse (3.4 ganymede) to work but I have had no success.

I have followed your instructions but still no luck, can you provide detailed instructions? i.e. which editor you use etc.

Thanks!

p.s. I have scoured the web & your post is the closest I have found to getting content assist to work for xsl documents.

Benjamin Francisoud said...

When I posted this article January 09, 2006 I think I was using Eclipse 3.3 web tools.

I need to make a better article that this, it's just various links that I dropped here to rememeber how I did it.


Hint: Try to close and reopen the xml editor between tests, sometimes it helps...

Benjamin Francisoud said...

New article is available here: http://francisoud.blogspot.com/2009/01/xsd-schema-validation-for-xsl-and-auto.html