RE: [xsl] XML to XML transformation

Subject: RE: [xsl] XML to XML transformation
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 02 Aug 2002 15:00:09 -0400
At 2002-08-02 11:19 -0700, Subramanian Subramanian wrote:
How can I maintain a map of java class name to output element
name in the XSL and use the map to generate the output element
name whenever I find a xsi:type attribute in the input xml.

Example
        Class                                   output element name
        com.integral.Car.BMW            bmwCar
        com.integral.Car.Buick          buickCar

Not many users of XSLT realize you can build such tables into your stylesheet using a namespace local to the stylesheet instance.


In the example below I build a table of the mappings and initialize a variable to contain all the members of the table ... I can then just "index" into the table by the attribute, to give me the element type (i.e. name) of the result tree node.

Another option would be to separate the table out to a stand-alone instance so that the maintenance of the table is separate from the maintenance of your stylesheet ... but you may want to have everything encapsulated in a single file.

Note that the empty string argument to the document() function returns the root of the node tree of the stylesheet as if it were a source tree instead of a stylesheet tree.

I hope this helps.

.................. Ken


T:\ftemp>type subramanian.xml <cars> <javaobject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:type="java:com.integral.Car.BMW" attr1="123"> <another-test/> </javaobject> <javaobject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:type="java:com.integral.Car.Buick"> <another-test/> </javaobject> <!--end of test--> </cars>

T:\ftemp>type subramanian.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
                xmlns:classtypes="dummy"
                exclude-result-prefixes="xsi classtypes"
                version="1.0">

<classtypes:mappings> <!--table of mappings from class string to type name-->
  <map class="java:com.integral.Car.BMW" type="bmwCar"/>
  <map class="java:com.integral.Car.Buick" type="buickCar"/>
</classtypes:mappings>

<xsl:variable name="maps" select="document('')/*/classtypes:mappings/map"/>

<xsl:template match="@xsi:*"/><!--suppress all attributes in XSI namespace-->

<xsl:template match="*[@xsi:type]"><!--elements of an XS type-->
  <xsl:element name="{$maps[@class=current()/@xsi:type]/@type}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="*"> <!--synthesize element with the input name-->
  <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="@*"> <!--synthesize attribute with the input name-->
  <xsl:attribute name="{name(.)}" namespace="{namespace-uri(.)}">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="comment()|processing-instruction()">
  <xsl:copy/>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xt subramanian.xml subramanian.xsl
<?xml version="1.0" encoding="utf-8"?>
<cars>
  <bmwCar attr1="123">
    <another-test/>
  </bmwCar>
  <buickCar>
    <another-test/>
  </buickCar>
<!--end of test-->
</cars>
T:\ftemp>rem Done!


-- Upcoming hands-on in-depth 3-days XSLT/XPath and/or 2-days XSL-FO: - North America: Sep 30-Oct 4,2002 - Japan: Oct 7-Oct 11,2002

G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0  +1(613)489-0999 (Fax:-0995)
ISBN 0-13-065196-6                       Definitive XSLT and XPath
ISBN 1-894049-08-X   Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1                Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books (electronic, printed),
articles, training (instructor-live,Internet-live,web/CD,licensed)
Next public training:           2002-08-05,26,27,09-30,10-03,07,10


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



Current Thread