RE: Xalan to transform only selected elements of XML

Subject: RE: Xalan to transform only selected elements of XML
From: Heather Lindsay <heather.lindsay@xxxxxxxxxxxxx>
Date: Wed, 31 May 2000 12:23:21 -0400
P.Dexter wrote:

My XML is:
---------
<?xml version="1.0"?>
<Policy>
  <Insured>
	<FirstName>John</FirstName>
	<MI>A.</MI>
	<LastName>Smith</LastName>
	<Address1>123 Bumpy Fast Lane</Address1>
	<State>MI</State>
	<Zip>12345</Zip>
  </Insured>
</Policy>

My XSL is:
---------
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:template match="Policy">
            <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="Insured">
            <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="FirstName">
        <b>First Name:</b>
        <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="LastName">
        <b>Last Name:</b>
        <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="Zip">
        <b>Zip:</b>
        <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

But, undesired Result is :
------------------------
<b>First Name:</b>John
A.
<b>Last Name:</b>Smith
123 Fast Lane
MI
<b>Zip: 12345</b>

My Desired Result:
------------------

<b>First Name:</b>John
<b>Last Name:</b>Smith
<b>Zip:</b>12345

How can I reach to my desired result?

Answer: Remember in my last response, I said that if you call
<xsl:apply-templates/> then templates will be called on ALL children.  If
you do not provide a template then the default template will be called.  The
default templates being called is why you get your undesired results.  If
you don't want templates called on ALL children then you have to "select"
which templates you want called.  An alternative is to create blank
templates for the other children.

Try this:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:template match="Policy">
            <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="Insured">
            <xsl:apply-templates select="FirstName | LastName | Zip"/>
  </xsl:template>

  <xsl:template match="FirstName">
        <b>First Name:</b>
        <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="LastName">
        <b>Last Name:</b>
        <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="Zip">
        <b>Zip:</b>
        <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

I hope this helps.  Get Mike Kay's XSLT book for a better understanding of
XSLT.  Also, there are some good tutorials out there (just make sure they
are tutorials for the newer namespace).

Good luck,
Heather


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


Current Thread