[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: xsl, and different xml versions

Subject: RE: xsl, and different xml versions
From: "Aron Bock" <aronbock@xxxxxxxxxxx>
Date: Fri, 15 Apr 2005 07:19:30 +0000
xmlns uri
Here's another way of achieving this, this one less intrusive on the main, dispatching sytlesheet than an earlier post which had it explicitly choose among different modes. This is xsl 1.0, and uses a [xalan] extension function.

The crux of the approach is to use an imported-stylesheet-specific "property", to affect which stylesheet to apply. The specific technique used isn't a mechanism of my invention or discovery, rather something I read previously ... dunno where ... and mundanely adapted as my journeyman knowledge allows. To wit:

1) each imported stylesheet defines an unique namespace-uri, and a 'starter' template that matches only that uri. All other templates use a mode="..." unique to that stylesheet. See doca-xsl, doc-b.xsl below.

2) the main stylesheet magically determines the unique-uri of the imported stylesheet to apply. This may be by examining an attribute in the input document, using a passed-in stylesheet param, doing a lookup in a referenced xml document, etc. See doc.xsl below

3) the main stylesheet dynamically creates a node with namespace-uri set to that determined above.

4) the main stylesheet calls <apply-templates select="...node-from-above..."/>; this invokes the starter template of the wanted imported stylesheet. The main stylesheet also passes in the root node of the input doc, via a param, for further processing.

5) once the starter template matches, it continues processing the rest of the document via apply-templates, selecting the root param from above.

This is illustrated below.

the input document ( doc.xml )
=====
<doc>
   <foo/>
   <bar/>
</doc>

the main stylesteet( doc.xsl ):
=====
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xalan="http://xml.apache.org/xalan"
exclude-result-prefixes="xalan"
>


   <xsl:import href="doc-a.xsl"/>
   <xsl:import href="doc-b.xsl"/>

<xsl:output method="xml"/>

   <xsl:template match="/">
       <!--
       Assume we've determined a namespace-uri unique
       to the xsl document to invoke.  This may be an
       attribute of the main xml document, may be passed
       in as a stylesheet parameter, etc.
       -->
       <xsl:variable name="xsldoc-uri" select="'doca-uri'"/>

       <xsl:variable name="selector">
           <xsl:element name="x" namespace="{$xsldoc-uri}"/>
       </xsl:variable>

       <xsl:apply-templates select="xalan:nodeset($selector)//*">
           <xsl:with-param name="root" select="/" />
       </xsl:apply-templates>
   </xsl:template>
</xsl:stylesheet>

an imported stylesheet (doc-a.xsl):
=====
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:doca="doca-uri"
exclude-result-prefixes="doca"
>


<xsl:output method="xml" />

   <xsl:template match="*[namespace-uri() = 'doca-uri']">
       <xsl:param name="root" select="/.."/>
       <xsl:apply-templates select="$root" mode="doca-uri"/>
   </xsl:template>

   <xsl:template match="/" mode="doca-uri">
       <doc>
       <xsl:apply-templates mode="doca-uri"/>
       </doc>
   </xsl:template>

   <xsl:template match="foo" mode="doca-uri">
       <foo v="a"/>
   </xsl:template>

   <xsl:template match="bar" mode="doca-uri">
       <bar v="a"/>
   </xsl:template>
</xsl:stylesheet>


an imported stylesheet( doc-b.xsl ):
=====
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:docb="docb-uri"
exclude-result-prefixes="docb"
>


<xsl:output method="xml"/>

   <xsl:template match="*[namespace-uri() = 'docb-uri']">
       <xsl:param name="root" select="/.."/>
       <xsl:apply-templates select="$root" mode="docb-uri"/>
   </xsl:template>

   <xsl:template match="/" mode="docb-uri">
       <doc>
       <xsl:apply-templates mode="docb-uri"/>
       </doc>
   </xsl:template>

   <xsl:template match="foo" mode="docb-uri">
       <foo v="b"/>
   </xsl:template>

   <xsl:template match="bar" mode="docb-uri">
       <bar v="b"/>
   </xsl:template>
</xsl:stylesheet>


When the variable doc.xsl :: xsldoc-uri is set to 'doca-uri' the result is:


<?xml version="1.0" encoding="UTF-8"?>
<doc>
   <foo v="a"/>
   <bar v="a"/>
</doc>

and when set to 'docb-uri'

<?xml version="1.0" encoding="UTF-8"?>
<doc>
   <foo v="b"/>
   <bar v="b"/>
</doc>


Regards,


--A

From: Jake Briggs <jakbri@xxxxxxxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  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...

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar  get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.