[xsl] Re: Re: Re: Combining stylesheets for baseclass-subclass type documents

Subject: [xsl] Re: Re: Re: Combining stylesheets for baseclass-subclass type documents
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 15 Apr 2002 07:19:20 -0700 (PDT)
Daniel Brockman wrote:

> >It seems to me that xsl:import and xsl:apply-imports were designed
> >exactly with the aim to solve such problems, or am I wrong?
> 
> No, you are right.  Indeed, I have used these elements to solve many
> *similar* problems.  However, in this specific case I simply can't
> seem to figure out how to go about doing it.
> 
> If you want to help me, see my previous message for corrected
versions
> of the files.

Sure.

You wanted this source.xml:

<a:a xmlns:a="urn:a" />

to be transformed by a stylesheet "a" to:

Result-a:
--------
<c:c xmlns:c="urn:c">
   <c:a-specific-information/>
</c:c>

and to transform it with a stylesheet "b2" to:

Result-b2:
---------
<c:c xmlns:c="urn:c">
   <c:a-specific-information/>
   <c:b-specific-information/>
</c:c>

or to transform it with a stylesheet "b3" to:

Result-b3:
---------
<c:c xmlns:c="urn:c">
   <c:a-specific-information>
      <c:b-specific-information/>
   </c:a-specific-information>
</c:c>


You also wanted the following:
"The first stylesheet, a.xsl, provides the a-specific information.  It
has to be independant of the second stylesheet, b.xsl, which provides
the b-specific information.  By definition, the second stylesheet,
b.xsl, cannot provide the a-specific information."

Here's the set of stylesheets, which solves your problem:

a:xsl
-----
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:a="urn:a"
 xmlns:c="urn:c"
 exclude-result-prefixes="a c">
 
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="a:a">
    <c:a-specific-information />
  </xsl:template>
</xsl:stylesheet>


b2.xsl:
------
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:a="urn:a"
 xmlns:c="urn:c"
 exclude-result-prefixes="a c">
 
 <xsl:import href="a.xsl"/>
 
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="a:a">
      <xsl:apply-imports />
      <c:b-specific-information/>
  </xsl:template>
</xsl:stylesheet>

b3.xsl:
------
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:v="http://icl.com/saxon";
 xmlns:a="urn:a"
 xmlns:c="urn:c"
 exclude-result-prefixes="a c v">
 
 <xsl:import href="a.xsl"/>
 
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="a:a">

    <xsl:variable name="rtf-basicResult">
      <xsl:apply-imports />
    </xsl:variable>
    
    <xsl:apply-templates select="v:node-set($rtf-basicResult)/*"
                         mode="post-processing"
    />
    
    
  </xsl:template>
  
  <xsl:template match="node() | @*" mode="post-processing">
    <xsl:copy>
      <xsl:if test="not(ancestor::*) and position() = 1">
        <c:b-specific-information/>
      </xsl:if>
      <xsl:apply-templates select="node() | @*"
                           mode="post-processing"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>


In order to obtain the three results above, process the source xml with
the following stylesheets:

container-a.xsl:
---------------
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:c="urn:c"
 exclude-result-prefixes="c">
 
 <xsl:import href="a.xsl"/>
 
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="/">
    <c:c xmlns:c="urn:c">
      <xsl:apply-templates />
    </c:c>
  </xsl:template>
</xsl:stylesheet>


When applied this transformation produces the first (basic) desired
result:

<c:c xmlns:c="urn:c">
   <c:a-specific-information/>
</c:c>

Then to produce the other two (extended) desired results, use:

container-b2.xsl:
----------------
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:c="urn:c"
 exclude-result-prefixes="c">
 
 <xsl:import href="b2.xsl"/>
 
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="/">
    <c:c xmlns:c="urn:c">
      <xsl:apply-templates />
    </c:c>
  </xsl:template>
</xsl:stylesheet>

When applied on the source xml it produces the first (extended) desired
result:

<c:c xmlns:c="urn:c">
   <c:a-specific-information/>
   <c:b-specific-information/>
</c:c>

and the following stylesheet,

container-b3.xsl:
----------------
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:c="urn:c"
 exclude-result-prefixes="c">
 
 <xsl:import href="b3.xsl"/>
 
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="/">
    <c:c xmlns:c="urn:c">
      <xsl:apply-templates />
    </c:c>
  </xsl:template>
</xsl:stylesheet>

when applied on the source xml produces the second desired (extended)
result:

<c:c xmlns:c="urn:c">
   <c:a-specific-information>
      <c:b-specific-information/>
   </c:a-specific-information>
</c:c>


Cheers,
Dimitre Novatchev.






__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/

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


Current Thread