Re: XSLT and imported schemas

Subject: Re: XSLT and imported schemas
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 13 Sep 2000 19:45:10 +0100
David,

>I have a XML schema "loop.xsd" which is importing
>another schema "common.xsd" using 
>
><import namespace="myURL"
>schemaLocation="common.xsd"/>
>
>Does anybody know hoe to apply a stylesheet to
>loop.xsd and include the content of common.xsd in the
>output. (It could be better if only the referenced
>elemets contained in common.xsd were included)

Well, you can identify the location of any imported schema files through
the XPath:

  /schema/import/@schemaLocation

You can then access those documents using the document() function:

  document(/schema/import/@schemaLocation)

This will give you the root node of the imported schema documents.  You can
apply templates to the content of these schemas just as you can to any
other source node, if you want to process them in some way:

<xsl:apply-templates
  select="document(/schema/import/@schemaLocation)/schema/*" />

or you can directly copy the content:

<xsl:copy-of select="document(/schema/import/@schemaLocation)/schema/*" />

If you want to only select those elements that are referenced within your
current schema, you need to have a way of identifying those references, and
I'd probably store them in a variable:

<xsl:variable name="references" select="//@ref" />

You can then select any element within your imported schema document that
has a name corresponding to one of those references:

  document(/schema/import/@schemaLocation)/schema/*[@name = $references]

Applying templates or copying only those elements will give you only the
referenced elements within the imported schema.

[Alternatively, you could define and use a key to retrieve the relevant
elements, which may be more efficient.]

Note that you can do recursive importing if you have a template like:

<xsl:template match="import">
  <xsl:apply-templates
    select="document(@schemaLocation)/schema/*[@name = $references or
                                               self::import]" />
</xsl:template>

I hope that this helps,

Jeni

Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread