[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Identity Transform and Default Templates, I think...

Subject: Identity Transform and Default Templates, I think...
From: "Schneeman, Brent" <BSchneem@xxxxxxxxxxx>
Date: Tue, 18 Jun 2002 13:13:58 -0700
bd template
[please forgive the length; I lack the experience to make this shorter]

I think that my question boils down to template application - when does a
particular template get applied? When does the default template get applied?

I'm trying to transform one XML document into another. An example is shown
(which is not the actual application, and may contain errors due to its
artificial nature):

<!--source document-->
<C3PO>
   <BD id="foo">
      <TR>
         <RF>The Right Foot</RF>
         <RK>The Right Knee</RK>
         <HR>The Heart</HR>
         <Ext id="bar"><ANewLimb>Some new Limb</ANewLimb></Ext>
      </TR>
   </BD>
</C3PO>

<!--transformed document-->
<SeeThreePO>
   <Body id="foo">
      <Torso>
         <Chest>
            <Heart>The Heart</Heart>
         </Chest>
         <RightLeg>
            <RightFoot>The Right Foot</RightFoot>
            <RightKnee>The Right Knee</RightKnee>
         </RightLeg>
         <Extension id="bar"><ANewLimb>Some new Limb</ANewLimb></Extension>
      </Torso>
   </Body>
</SeeThreePO>

Basically, the problem is one of element-expansion ("C3PO" to "SeeThreePO")
and heirarchy changing (introducing the "Chest" element, for example). I use
the following XSL style sheet to make the transformation:

<xsl:sytlesheet>

   <!--from
   http://www.dpawson.co.uk/xsl/sect2/identity.html#d142e45-->
   <!--named .resource for build purposes...-->

   <xsl:import href="identity.resource"/>

  <!-- match the root and select the C3PO template -->
  <xsl:template match="/">
    <xsl:apply-templates select="C3PO"/>
  </xsl:template>

  <!-- the C3PO template -->
  <xsl:template match="C3PO">
    <!-- convert C3PO to SeeThreePO -->
    <SeeThreePO>
      <!-- and apply the template for the BD element -->
      <xsl:apply-templates select="BD"/>
    </SeeThreePO>
  </xsl:template>

  <!-- BD Template -->
  <xsl:template match="BD">
    <!-- output Body element -->
    <Body>
      <!-- grab the id attribute -->
      <xsl:attribute name="id">
        <xsl:value-of select="attribute::id"/>
      </xsl:attribute>
      <xsl:apply-templates select="TR"/>
    </Body>
  </xsl:template>

  <xsl:template match="TR">
    <Torso>
      <Chest>
        <xsl:apply-templates select="HR"/>
      </Chest>
      <RightLeg>
        <xsl:apply-templates select="RF"/>
        <xsl:apply-templates select="RK"/>
      </RightLeg>
      
      <xsl:apply-templates select="Ext"/>
    </Torso>
  </xsl:template>

    <xsl:template match="Ext">
    <Extension>
      <xsl:attribute name="id">
        <xsl:value-of select="attribute::id"/>
      </xsl:attribute>
      <!--pickup any sub elements, using the identity transform-->
      <xsl:apply-templates/>
    </Extension>
  </xsl:template>

 <xsl:template match="HR">
    <Heart>
      <xsl:apply-templates/>
    </Heart>
  </xsl:template>

   <xsl:template match="RF">
    <RightFoot>
      <xsl:apply-templates/>
    </RightFoot>
  </xsl:template>

   <xsl:template match="RK">
    <RightKnee>
      <xsl:apply-templates/>
    </RightKnee>
  </xsl:template>

</xsl:stylesheet>

That sheet works fine (using Xalan2-J). But now I'd like to make it a bit
more flexible. I want to be able to identically copy elements which are not
matched by any explicit template. Something like:

<C3PO>
   <Soul/>
   <BD id="foo">
	<Head><RightEye>The Right Eye</RightEye></Head>
      <TR>
         <RF>The Right Foot</RF>
         <RK>The Right Knee</RK>
         <HR>The Heart</HR>
         <Ext id="bar"><ANewLimb>Some new Limb</ANewLimb></Ext>
      </TR>
   </BD>
</C3PO>

The "Soul" and "Head" elements should be copied into the output document. If
I add a xsl:template element into the XSL like so:

 <!-- BD Template -->
  <xsl:template match="BD">
    <!-- output Body element -->
    <Body>
      <!-- grab the id attribute -->
      <xsl:attribute name="id">
        <xsl:value-of select="attribute::id"/>
      </xsl:attribute>
      <xsl:apply-templates select="TR"/>
	<!-- pickup any new non-TR elements using identity -->
	<xsl:apply-templates/>
    </Body>
  </xsl:template>

I wind up getting the transformed content of the TR, but not the "Head"
element:

<SeeThreePO>
   <Body id="foo">
      <Torso>
         <Chest>
            <Heart>The Heart</Heart>
         </Chest>
         <RightLeg>
            <RightFoot>The Right Foot</RightFoot>
            <RightKnee>The Right Knee</RightKnee>
         </RightLeg>
         <Extension id="bar"><ANewLimb>Some new Limb</ANewLimb></Extension>
      </Torso>
	<Heart>The Heart</Heart>
	<RightFoot>....
	<RightKnee>....
   </Body>
</SeeThreePO>

Is this clear as to what I'm trying to accomplish? Any thoughts on how to go
about it?



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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.