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

RE: recreating elements with attributes

Subject: RE: recreating elements with attributes
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 11 Feb 2004 11:41:55 -0500
xsl copy attributes
Hi Terry,

At 07:42 PM 2/10/2004, you wrote:
<xsl:template match="CDRS/TOLL_CDR">
<TOLL_CDR>
<xsl:for-each select="@*">
<xsl:choose>
<xsl:when test="name()='DIVISION'">
<!-- do special logic here, such as rename an attribute -->
<xsl:attribute name="mydivision">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:copy/>
</xsl:otherwise>
</xsl:choose>


           </xsl:for-each>
        </TOLL_CDR>
   </xsl:template>

This would be expressed more idiomatically as follows:


<xsl:template match="CDRS/TOLL_CDR">
  <TOLL_CDR>
    <xsl:apply-templates select="@*">
  </TOLL_CDR>
</xsl:template>

<xsl:template match="TOLL_CDR/@DIVISION" priority="2">
  <xsl:attribute name="mydivision">
    <xsl:value-of select="." />
  </xsl:attribute>
</xsl:template>

<xsl:template match="TOLL_CDR/@*">
  <xsl:copy-of select="."/>
</xsl:template>


In this case the priority is needed on the template matching the @DIVISION attribute since both it and the template matching "TOLL_CDR/@*" (i.e., all attributes on TOLL_CDR) have the same default priority (a priority of 1). Since the first template, but not the second, matches the attribute whose name you want to change, giving it a higher priority ensures that it will match when it can, whereas the other attributes on TOLL_CDR will only match the second template.


(The matches on the latter two templates might as well be "@DIVISION" and "@*", in which case you wouldn't need to set the priority since "@DIVISION" has a default priority of 0.5, while "@*" has a priority of 0. But in this case they would also match attributes on other elements; whether this is a good thing depends on what the rest of your transformation is doing.)

There are other ways of doing this too, offering different tradeoffs of conciseness, flexibility and extensibility; this particular approach, however, may help shed light on XSLT's funky and powerful node-manipulation processing model.

Cheers,
Wendell



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


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.