Re: [xsl] XML to XML transformation

Subject: Re: [xsl] XML to XML transformation
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 02 Aug 2002 13:13:48 -0400
At 2002-08-02 09:45 -0700, Subramanian Subramanian wrote:
I need to transform one XML to another XML. Some of the elements in the
source XML has an attribute xsi:type and the value is a java class name. In
the output xml I want to remove this attribute and change the name of the
element to something based on the java class name.

Synthesize the new element's type from the attribute value, then strip the attribute. Note the translate() function is handy for what you need to do.


It is common to neglect re-synthesizing all of the elements because of the ancestral namespace declarations adding namespace nodes to the elements in the source node tree.

It is also common when looking at this problem to be incorrectly thinking about "changing the markup" instead of thinking about the node trees created from the source markup and used to create the output markup.

A working example is below.

I hope this helps.

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

T:\ftemp>type subramanian.xml
 <javaobject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
        xsi:type="java:a.b.c"
       attr1="123">
   <a-subelement>Test</a-subelement>
   <!--end of test-->
  </javaobject>

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";
                exclude-result-prefixes="xsi"
                version="1.0">

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

<xsl:template match="*[@xsi:type]"><!--elements of an XS type-->
  <xsl:element name="{translate( substring-after( @xsi:type, 'java:' ),
                                 '.', '' )}">
    <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 theinput 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"?> <abc attr1="123"> <a-subelement>Test</a-subelement> <!--end of test--> </abc> T:\ftemp>


-- 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