Sign In/My Account | View Cart  
advertisement

Article:
 Transforming XML Schemas
Subject: Which extensions needed for imports (includes)
Date: 2004-07-21 03:45:44
From: mattad

Which XSLT source code do I have to add to enable the XSLT script to handle imports (resp. includes) as well?


Is there somewhere a complete script available? I don't want to re-invent the wheel. I could imagine that a full XSLT script has been developed meanwhile for such a standard task.


Matt


Previous Message Previous Message   Next Message Next Message


Titles Only Titles Only Newest First
  • Which extensions needed for imports (includes)
    2004-07-21 09:51:20 Eric Gropp

    We've chosen a subset of features to use in our schemas, so we do not have a set of templates that can handle all schema features. However, here's a description of our approach, which I hope may be of some use. I'm sure there are better ones out there, like http://www.idealliance.org/xmlusa/03/call/xmlpapers/03-03-04.994/.03-03-04.html .


    We've handled referenced schema files by creating a node-set of all the schemas. We can then resolve an Element References, Named Types, etc by selecting from the definitions in the schemas in this node set.


    At the top of the stylesheet, we define two XSLT variables: the first variable creates a result set of all the schemas, and the second contains a node set. You'll have to use your favorite equivalent of the node-set extension function. If your XSLT is working off of an instance document, you can use a xsi:schemaLocation attribute as a starting point.



    <xsl:variable name="schemaSetTree">
    <xsl:apply-templates select="/xs:schema" mode="resolveSchemas"/>
    </xsl:variable>
    <xsl:variable name="schemaSet" select="msxsl:node-set($schemaSetTree)"/>



    The "resolveSchemas" mode contains a template that walks through all the tree of included and imported schemas:



    <xsl:template match="xs:schema" mode="resolveSchemas">
    <xsl:param name="targetNamespace" select="@targetNamespace"/>


    <xs:schema targetNamespace="{$targetNamespace}">
    <xsl:copy-of select="*"/>
    </xs:schema>


    <xsl:apply-templates select="document(xs:include/@schemaLocation)/*" mode="resolveSchemas">
    <xsl:with-param name="targetNamespace" select="$targetNamespace"/>
    </xsl:apply-templates>


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


    </xsl:template>



    For convenience, all included schemas are given a @targetNamespace value equal to the parent namespace-uri.


    You can then use the $schemaSet variable to resolve schema definitions. For example, the complex type definition "Dog" in the namespace "uri:pets" can be found using:


    $schemaSet/xs:schema[@targetNamespace='uri:pets']/xs:complexType[@name='Dog']



    Yours,


    Eric G.
    Portland, OR






Sponsored By: