Re: [xsl] XSLT 2.0 question

Subject: Re: [xsl] XSLT 2.0 question
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 18 Mar 2002 09:35:30 +0000
Hi Bryan,

> am wondering if it would be possible to set up solutions that do the
> following in xslt 2.0, let us say you run your xslt against an xml
> document that contains inline xslt code, perhaps just a single
> xsl:template, would there be any way, hopefully incredibly involved
> and complex :) to declare that inline templates should be run and
> take precedence over templates in the xslt.

Yep, they're called embedded stylesheets. See
http://www.w3.org/TR/xslt#section-Embedding-Stylesheets in XSLT 1.0 or
http://www.w3.org/TR/xslt20/#embedded in the XSLT 2.0 WD.

Say that your stylesheet was called 'stylesheet.xsl', you could do:

<doc>
  <person>...</person>
  
  <xsl:stylesheet version="1.0" id="stylesheet"
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
                  
    <xsl:import href="stylesheet.xsl" />
    
    <xsl:template match="person">
      do something
    </xsl:template>

  </xsl:stylesheet>
</doc>

If you use the embedded stylesheet (accessing it by its ID), then it
imports your main stylesheet (stylesheet.xsl) and overrides its
content in just the same way as importing a stylesheet does normally.
So your local style information for the person element has priority.

To make this work, you have to declare the id attribute on
xsl:stylesheet as an ID attribute, so the top of the document will
probably actually look like:

<?xml version="1.0"?>
<!DOCTYPE doc [
<!ELEMENT xsl:stylesheet ANY>
<!ATTLIST xsl:stylesheet id ID #REQUIRED>
]>
<?xml-stylesheet type="text/xsl" href="#stylesheet"?>
<doc>
  <person>...</person>
  
  <xsl:stylesheet version="1.0" id="stylesheet"
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
                  
    <xsl:import href="stylesheet.xsl" />
    
    <xsl:template match="person">
      do something
    </xsl:template>

  </xsl:stylesheet>
</doc>

Cheers,

Jeni

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


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


Current Thread