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

Re: Identity Transform and Default Templates, I think.

Subject: Re: Identity Transform and Default Templates, I think...
From: "Schneeman, Brent" <BSchneem@xxxxxxxxxxx>
Date: Wed, 19 Jun 2002 07:52:09 -0700
identity xsl
Thanks to J.Pietschmann, David Carlisle, Mike Brown for responding. I really
appreciate it. (Mike, I hope all is well with you and yours with respect to
the fires)

I took the time to make an operational model, incorporating the changes
suggested. And I got it to work. Thanks.

Here's the original source document again:

<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>

Here's the updated and operational XSL (it imports the canonical copy
transform (e.g. identity transform)):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<!--from http://www.dpawson.co.uk/xsl/sect2/identity.html#d142e45-->
<xsl:import href="./identity.xsl"/>

<xsl:output indent="yes"/>


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

<!--BD Template -->
<xsl:template match="BD">
  <!-- output Body element -->
  <Body id="{@id}"> 
    <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 id="{@id}">
    <!--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>

Identity.xsl looks like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <!-- The identity Transform -->
  <!-- Whenever you match any node or any attribute -->
  <xsl:template match="node()|@*">
    <!-- Copy the current node -->
    <xsl:copy>
      <!-- Including any attributes it has and any child nodes -->
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>


</xsl:stylesheet>

Applying the XSL to the XML generates this output (pretty printed):

<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>

That all works like a champ. (I used XALAN's built-in Process class to do
this)

Now, I want to transform this document:

<C3PO>
   <SOUL>The Soul</SOUL>
   <BD id="foo">
      <HEAD>The Head</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>

and make sure that the SOUL and HEAD elements are copied. If I take the
suggestion to replace only the  <xsl:apply-templates select"xxx"> elements
within the C3PO and BD templates with <xsl:apply-templates/>, I get the SOUL
and HEAD:

<SeeThreePO>
   <SOUL>The Soul</SOUL>
   <Body id="foo">
      <HEAD>The Head</HEAD>
      <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>

Victory! Now I need to apply the lesson to the actual application. Thanks
bunches.

Note that if I globally replace the <xsl:apply-templates select"xxx"> with
<xsl:apply-templates/>, the output winds up with an extra RightFoot and
RightKnee in the Chest along with the Heart, and an extra Heart in the
RightLeg.

Regards,
-Brent Schneeman



 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.