This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: XML to XML transformation
- From: Subramanian Subramanian <subramanian dot subramanian at integral dot com>
- To: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Date: Fri, 2 Aug 2002 11:19:55 -0700
- Subject: RE: [xsl] XML to XML transformation
- Reply-to: xsl-list at lists dot mulberrytech dot com
Ken,
Thanks for the quick response. I have one more question.
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
Input xml:
<cars>
<javaobject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="java:com.integral.Car.BMW" attr1="123">
:
</javaobject>
<javaobject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="java:com.integral.Car.Buick">
:
</javaobject>
</cars>
Expected output -
<cars>
<bmwCar attr1="123">
:
</bmwCar>
<buickCar>
:
</buickCar>
</cars>
Subramanian
-----Original Message-----
From: G. Ken Holman [mailto:gkholman@CraneSoftwrights.com]
Sent: Friday, August 02, 2002 10:14 AM
To: xsl-list@lists.mulberrytech.com
Subject: Re: [xsl] XML to XML transformation
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@CraneSoftwrights.com
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
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list