xsl-list
[Top] [All Lists]

RE: recreating elements with attributes

2004-02-11 09:41:55
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(_at_)mulberrytech(_dot_)com
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



<Prev in Thread] Current Thread [Next in Thread>