Re: [xsl] xsl, and different xml versions

Subject: Re: [xsl] xsl, and different xml versions
From: Jake Briggs <jakbri@xxxxxxxxxxxxxx>
Date: Fri, 15 Apr 2005 15:14:36 +1200
Thanks for the reply. How do we know which "mode" the input file (doc.xml) should be transformed in?
Should it not be like this :


The input file (doc.xml):
=====
<doc version="a"> ######## Note the version attribute ########
<foo/>
</doc>

and the main xsl :
The main xsl (doc.xsl):
=====
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:import href="doc-a.xsl"/>
<xsl:import href="doc-b.xsl"/>


<xsl:output method="xml"/>

<xsl:template match="/">
<xsl:variable name="mode" select="@version"/> ######## instead of <xsl:variable name="mode" select="'b'"/> #########
<xsl:choose>
<xsl:when test="$mode = 'a'">
<xsl:apply-templates mode="a"/>
</xsl:when>
<xsl:when test="$mode = 'b'">
<xsl:apply-templates mode="b"/>
</xsl:when>
</xsl:choose>


</xsl:template>
</xsl:stylesheet>


Or even better, the main.xsl could look like this :


The main xsl (doc.xsl):
=====
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:import href="doc-a.xsl"/>
<xsl:import href="doc-b.xsl"/>


<xsl:output method="xml"/>

<xsl:template match="/">
<xsl:apply-templates mode="@version"/>
</xsl:template>
</xsl:stylesheet>


Aron Bock wrote:


Jake, the "poor man's solution" is to use modes, as follows: we'll transform doc.xml (below) using doc.xsl; doc.xsl imports 2 xsl files doc-a.xsl and doc-b.xsl with their respective templates marked as mode="a" and mode="b". Please see below:

The input file (doc.xml):
=====
<doc>
<foo/>
</doc>


Imported file (doc-a.xsl):
=====
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:output method="xml"/>


<xsl:template match="foo" mode="a">
<foo v="a"/>
</xsl:template>
</xsl:stylesheet>

Imported file (doc-b.xsl):
=====
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:output method="xml"/>


<xsl:template match="foo" mode="b">
<foo v="b"/>
</xsl:template>
</xsl:stylesheet>

The main xsl (doc.xsl):
=====
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:import href="doc-a.xsl"/>
<xsl:import href="doc-b.xsl"/>


<xsl:output method="xml"/>

<xsl:template match="/">
<xsl:variable name="mode" select="'b'"/>
<xsl:choose>
<xsl:when test="$mode = 'a'">
<xsl:apply-templates mode="a"/>
</xsl:when>
<xsl:when test="$mode = 'b'">
<xsl:apply-templates mode="b"/>
</xsl:when>
</xsl:choose>

</xsl:template>
</xsl:stylesheet>

A more sophisticated and elegant--if less simple and transparent--approach would be to dynamically dispatch processing to different stylesheets based on a stylesheet-specific "property".

Regards,

--A

From: Jake Briggs <jakbri@xxxxxxxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] xsl, and different xml versions
Date: Fri, 15 Apr 2005 14:27:34 +1200

Hi all

I have a problem and I am unsure which approach to take in order to solve it. The problem is this:

The root element of the xml I am transforming with xsl will have a version number in it, indicating which version of the xsl i want to transform it with. By that, I mean I have 2 style sheets, both operating on similer xml. I though I may be able to do this by xsl:import, when the xsl matches the root element I though that I could make a decision based on the value of the attribute. IE :

The source xml :

<report version="1">
<table foo="bar"/>
<text value="sometext"/>
</report>

The xsl :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>


<xsl:template match="/">
<xsl:if test="@version = '1'"> <xsl:import href="version1.xsl"/>
</xsl:if>
<xsl:if test="@version = '2'"> <xsl:import href="version2.xsl"/>
</xsl:if>

<xsl:apply-imports/>
</xsl:template>

</xsl:stylesheet>


The files version1.xsl, and version2.xsl would contain matches for "table" and "text", but would process them differently.


But no, apparently <xsl:import /> and <xsl:include /> can only have <xsl:stylesheet /> as a parent. I would like to do something like the above, because I dont want to have to call my version 2 xml elements convoluted names so that they dont conflict with the version 1 xml. I dont want to have a version attribute in each xml element either. Also, I would prefer to avoid having the software that initiates the transformation make the decision on which stylesheet to use to transform the xml, but maybe that is the correct way to do it.

Any ideas?

Jake

--
Named after its country of origin 'England', English is a little known dialect used by up to 1.5 billion non-Americans worldwide. Some interesting but obviously incorrect features of the language include:


- queues of people
- wonderful coloUrs
- the useful metal aluminIum
- the exotic herbs (h-urbs), basil (ba-zil) and oregano (o-re-gaa-no)
- specialiSed books called 'dictionaries' that tell you how to spell words correctly


Many people using this bizarre gutter speak also subscribe to the pagan belief that water freezes at 0 degrees and that distances should be measured in the forbidden mathematical system of base-10...


_________________________________________________________________
Dont just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/



-- Named after its country of origin 'England', English is a little known dialect used by up to 1.5 billion non-Americans worldwide. Some interesting but obviously incorrect features of the language include:

- queues of people
- wonderful coloUrs
- the useful metal aluminIum
- the exotic herbs (h-urbs), basil (ba-zil) and oregano (o-re-gaa-no)
- specialiSed books called 'dictionaries' that tell you how to spell words correctly

Many people using this bizarre gutter speak also subscribe to the pagan belief that water freezes at 0 degrees and that distances should be measured in the forbidden mathematical system of base-10...

Current Thread