[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Stylesheet from a stylesheet


Subject: [xsl] Stylesheet from a stylesheet
From: Midsummer Sun <midsummer.sun@xxxxxxxxx>
Date: Sun, 13 Mar 2005 23:51:27 +0530

Friends,
  I have to write a XSLT stylesheet from another XSLT stylesheet(a
kind of tool). This question is in continuation to my earlier question
titled "CDATA help".

I'll try to explain my exact requirements with an example ..

Lets say one XML file is [1]
<x>
 <p a="1">1</p>
 <q b="2">2</q> 
</x>

and a second XML file is [2]
<x>
 <u>
   <w m="1">1</w>
 </u>
 <v n="2">2</v> 
</x>

And, there is a mapping XML file (lets say map.xml) like this -
<schemamap>
 <!-- element mapping -->
 <map>
   <xpath1>/x/p</xpath1>
   <xpath2>/x/u/w</xpath2>
 </map>
 <map>
   <xpath1>/x/q</xpath1>
   <xpath2>/x/v</xpath2>
 </map>
 ........
 ........
 <!-- attribute mapping -->
 <map>
   <xpath1>/x/p/@a</xpath1>
   <xpath2>/x/u/w/@m</xpath2>
 </map>
 <map>
   <xpath1>/x/q/@b</xpath1>
   <xpath2>/x/v/@n</xpath2>
 </map>
 ........
 ........

 <!-- (possible) namespace mappings -->
 ........
 ........
</schemamap>

The XSLT stylesheet (the tool) will take map.xml file as an input and
generate an XSLT stylesheet as output, that will be able to transform
any XML from format [1] to format [2]  .

For e.g. the tool (a XSLT stylesheet) will generate this output (lets
say file gen.xslt) , when it is given as input the file, map.xml -
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/x/p">
    <x>
      <u>
        <w><xsl:value-of select="." /></w>
      </u>
    </x>
  </xsl:template>
  <xsl:template match="/x/q">
    <x>
      <v>
        <xsl:value-of select="." />
      </v>
    </x>
  </xsl:template>
  <xsl:template match="/x/p/@a">
    <xsl:attribute name="m">
      <xsl:value-of select="." />
    </xsl:attribute>    
  </xsl:template>
  ....
  ....
</xsl:stylesheet>

When gen.xslt will be applied to any XML file with format [1] the
output should be an XML file with format [2]. Please note that the
above generated XSLT by the tool would probably produce output -
<x>
  <u>
    <w>1</w>
  </u>
</x>
<x>
  <v>
    <w>2</w>
  </v>
</x>
etc..

But it should actually be -
<x>
  <u>
    <w>1</w>
  </u>
  <v>2</v>
</x>
(i.e. nested tags should be taken care, and also attribute mappings
and possible namespace mappings).

The XML files [1] and [2] are dynamic and what they will contain is unknown. 

It seems that XPath expressions are the best way to specify mapping
information between 2 XML formats.. The mapping file is under my
control, so I am free to design it in a better way.. Any suggestion of
specifying mapping information in any other way is welcome..

Presently my target is to map "elements" and "the attributes". But it
is possible that namespaces from format [1] and format[2] may also
need to be mapped. So I have to cater to mapping namespaces also. I
need suggestions of specifying mapping information for namespaces in
map.xml (I am presently unable to think a suitable way for this).

Hope my requirements are clear..

Based on the help I recieved earlier, I believe the tool(a XSLT
stylesheet) will roughly look like -
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output method="xml" indent="yes" />

 <xsl:template match="/">
   <xsl:element name="xsl:stylesheet"
namespace="http://www.w3.org/1999/XSL/Transform">
     <xsl:attribute name="version">1.0</xsl:attribute>
     .....

     .....

     .....
   </xsl:element>
 </xsl:template>

</xsl:stylesheet>


At present I am looking for XSLT 1.0 solution. I am open to using
extension functions.

If its not achievable with XSLT 1.0(possibly with extensions), I am
then open to XSLT 2.0 ideas.. But please give XSLT 2.0 ideas also!

Waiting for some ideas..

Best regards,


Current Thread
Keywords