RE: [xsl] an elements schema context via xslt

Subject: RE: [xsl] an elements schema context via xslt
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 30 Jun 2005 23:58:37 +0100
> I wonder if it is possible (with a reasonable amount of code) to find
> out (given a xml document, the xpath to an element in this doc and the
> schema (xsd) document upon which the document is/was created), to find
> out which are the possible child- (and sibling- and attribute-)
> elements for the given element - using xslt.

If you read the schema spec, you will see that there's a significant amount
of work a schema processor has to do to build a schema (= a set of type
definitions) from a schema document (an xs:schema element in a source XML
document). You don't want to try writing this code yourself in XSLT. For
example, you would have to analyze model groups, attribute groups, and
substitition groups, all recursively, you would have to understand schema
imports and namespaces, wildcards, subtypes derived by extension, and a host
of other things.

(Incidentally, some schema-aware XML editors clearly don't do this very
well.)

Your better bet is to access a compiled schema using extension functions. It
shouldn't be too difficult to do this using Saxon-SA.

Also, having a path expression that identifies an element doesn't make it
especially easy to work out which element declaration or type definition is
applicable to that element. A schema-aware processor will validate the
source document and annotate each node with a reference to the schema type
that was used to validate it: with some use of extension functions, you can
get from this type annotation to the definition of the schema type in the
compiled schema, and from this to the list of permitted children.

Michael Kay
http://www.saxonica.com/

Current Thread