Re: [xsl] How Do I Exclude Portions Of XML When XForming XML To XML

Subject: Re: [xsl] How Do I Exclude Portions Of XML When XForming XML To XML
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 24 Feb 2004 23:31:10 -0500
At 2004-02-24 15:16 -0800, Gary Fix wrote:
> I have a sample XML file and I want to transform this to a different XML file while omitting part of the original xml.

I see this as a mapping issue where omission is the mapping of something to nothing. Using keys to store the mappings from an external file would allow you to have multiple mappings for multiple files with the same stylesheet.

> Any help would be appreciated.

I hope the example below helps.

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

T:\ftemp>type gary.xml
T:\ftemp>type mappings.xml
<?xml version="1.0" encoding="iso-8859-1"?>
   <mapping old="Auto" new="Vehicle"/>
   <mapping old="BasicVehInfo" new="VehicleData"/>
   <mapping old="ModelYear" new="VehicleYear"/>
   <mapping old="VehIdentificationNumber" new="VIN"/>

T:\ftemp>type gary.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="";

<xsl:output indent="yes"/>

<xsl:key name="names" match="mapping/@new" use="../@old"/>

<xsl:template match="*">
   <xsl:variable name="oldname" select="name(.)"/>
   <xsl:variable name="newname">
     <xsl:for-each select="document('mappings.xml')">
       <xsl:value-of select="key('names',$oldname)"/>
   <xsl:if test="string($newname)">
     <xsl:element name="{$newname}">
       <xsl:apply-templates select="@*|node()"/>

<xsl:template match="@*|text()"><!--identity for all other nodes-->
     <xsl:apply-templates select="@*|node()"/>
                                          <!--this cleans up the output-->
<xsl:template match="text()[not(normalize-space())]"/>


T:\ftemp>saxon gary.xml gary.xsl
<?xml version="1.0" encoding="utf-8"?>

-- Public courses: Spring 2004 world tour of hands-on XSL instruction Each week: Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO United States: Washington, DC March 15; San Francisco, CA March 22 Finland April 26; Hong Kong May 17; Germany May 24; London June 07 World-wide on-site corporate, government & user group XML training

G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness

XSL-List info and archive:

Current Thread