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: "Gary Fix" <gary.fix@xxxxxxxxxx>
Date: Wed, 25 Feb 2004 07:45:10 -0800
Thanks Ken! Your solution worked great!
:-)

-----Original Message-----
From: G. Ken Holman [mailto:gkholman@xxxxxxxxxxxxxxxxxxxx]
Sent: Tuesday, February 24, 2004 8:31 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] How Do I Exclude Portions Of XML When XForming XML To
XML


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
<Auto>
         <CustId>
                 <SPName>com.anysystem</SPName>
                 <CustPermId/>
                 <CustLoginId>fred</CustLoginId>
         </CustId>
         <BasicVehInfo>
                 <ModelYear>1994</ModelYear>
                 <VehIdentificationNumber>1ABCD123456</VehIdentificationNumber>
         </BasicVehInfo>
</Auto>
T:\ftemp>type mappings.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<mappings>
    <mapping old="Auto" new="Vehicle"/>
    <mapping old="BasicVehInfo" new="VehicleData"/>
    <mapping old="ModelYear" new="VehicleYear"/>
    <mapping old="VehIdentificationNumber" new="VIN"/>
</mappings>

T:\ftemp>type gary.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                  version="1.0">

<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:for-each>
    </xsl:variable>
    <xsl:if test="string($newname)">
      <xsl:element name="{$newname}">
        <xsl:apply-templates select="@*|node()"/>
      </xsl:element>
    </xsl:if>
</xsl:template>

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

</xsl:stylesheet>

T:\ftemp>saxon gary.xml gary.xsl
<?xml version="1.0" encoding="utf-8"?>
<Vehicle>
    <VehicleData>
       <VehicleYear>1994</VehicleYear>
       <VIN>1ABCD123456</VIN>
    </VehicleData>
</Vehicle>
T:\ftemp>


--
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.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc


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

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


Current Thread