Re: [xsl] Sort Question

Subject: Re: [xsl] Sort Question
From: omprakash.v@xxxxxxxxxxxxx
Date: Thu, 2 Jun 2005 13:26:51 +0530
Hi,

    Please find below the stylesheet that should solve your problem.

Just in case you would like to know:

Problems : The first element ColorTypeRed should be simply Red. I can't
figure out why the
ColorType is being injected here. If I remove the sort I get the correct
structure (in the wrong order).


Ans: My first thought was the ColorType came from the lone EnumeratedType
child of the tblEnumeratedTypes_WC element and modified the xsl. This
worked perfectly and my guess was proven right.



Next, the Range_Type should be

RANGE_TYPE ( UINT8, ColorType, Red, White ) ;

I can't for the life of me figure out how to restucture the template

           <xsl:template match="tblEnumeratedTypes_WC" mode="range-macro">


Ans: This one was a lot tougher. My original idea was to store the sorted
nodes in a variable and access the nodes using xpath expressions. But this
didn't work as I  found out to my surprise that the text output method does
not allow you create elements not even create them and bind them to a
variable. Then I  found the match template for tblEnumValues_WC node  was
being called in sorted order and used position() arrive at the desired
result. However, the stylesheet needs to receive the position of the last
node as a parameter or needs to compute it in a previous pass to allow the
stylesheet to scale.


Hope this helps.

Cheers,
prakash




<?xml version="1.0" encoding="ISO-8859-1"?>
<?altova_sps C:\Documents and Settings\simonj\My
Documents\us101\xml\Messages.sps?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:xalan
="http://xml.apache.org/xalan";
>
           <xsl:output method="text"/>
           <xsl:strip-space elements="*"/>

           <xsl:param name="file" select=" 'MESSAGES.h' "/>

           <xsl:variable name="assignment" select=" ' = ' "/>
           <xsl:variable name="terminator" select="  ' ;' "/>

           <xsl:template match="dataroot">
                               <xsl:apply-templates select="node()"/>
           </xsl:template>


<!--
-->
<!-- This template matches an Enumerated Type                -->
<!--
-->
<!--           It creates a header for the type and then     -->
<!--           creates the C struct.
-->
<!--
-->

           <xsl:template match="tblEnumeratedTypes_WC" priority="1">
                     <xsl:choose>
                               <xsl:when test="1=position()">
                                          <xsl:call-template name
="SectionHeader">
                                                    <xsl:with-param
name="SectionName" >Enumerated Types</xsl:with-param>
                                          </xsl:call-template>
                               </xsl:when>
                     </xsl:choose>
                     <xsl:apply-templates select="." mode="doc"/>
                     <xsl:apply-templates select="." mode="struct"/>
                     <xsl:apply-templates select="." mode="range-macro"/>
           </xsl:template>


<!--
-->
<!--      This template is used to create the Section Headers
-->
<!--
-->

           <!-- Should Be Centered ?? -->
           <xsl:template name="SectionHeader">
                     <xsl:param name="SectionName"/>

//----------------------------------------------------------------------
----------------

//----------------------------------------------------------------------
----------------
           //        <xsl:value-of select="$SectionName"/>

//----------------------------------------------------------------------
----------------

//----------------------------------------------------------------------
----------------
           </xsl:template>



<!--
-->
<!--      This template creates the header for an enumerated Type
-->
<!--
-->

           <xsl:template match="tblEnumeratedTypes_WC" mode="doc">

//----------------------------------------------------------------------
----------------
           // Name                  : <xsl:apply-templates select
="EnumeratedType"/>
           // Description    : <xsl:apply-templates select="Description"/>


//----------------------------------------------------------------------
----------------
           </xsl:template>




<!--
-->
<!--      This template creates the structure for an enumerated Type
-->
<!--
-->
           <xsl:template match="tblEnumeratedTypes_WC" mode="struct">

           enum {
                               <xsl:apply-templates select
="tblEnumValues_WC">
                                          <xsl:sort select="EnumValue"/>
                               </xsl:apply-templates>
           }   ;
           </xsl:template>

<!--
-->
<!--      This template creates the line for each of the enumerations
within         -->
<!--      an Enumerated Type
-->
<!--
-->

           <xsl:template match="tblEnumValues_WC">
                     <xsl:call-template name="padSpaces">
                               <xsl:with-param name="text" >
                                          <xsl:call-template name
="removeSpaces">
                                                    <xsl:with-param name
="text"
select="EnumValueDescription"/>
                                          </xsl:call-template>
                               </xsl:with-param>
                               <xsl:with-param name="size"
>20</xsl:with-param>
                     </xsl:call-template>

                     <xsl:text> = </xsl:text>

                     <!-- If we are on the last line, dont add a "," or
carriage return -->
                     <xsl:value-of select="EnumValue"/>
                     <xsl:choose>
                               <xsl:when test="last()=position()">
                               </xsl:when>

                               <xsl:otherwise>
                                          <xsl:text>, &#x0a;
</xsl:text>
                               </xsl:otherwise>
                     </xsl:choose>

           </xsl:template>



<!--
-->
<!--      This template creates call to the RANGE_TYPE Macro for an
enumerated Type     -->
<!--
-->

           <xsl:template match="tblEnumeratedTypes_WC" mode="range-macro">

           <xsl:variable name="ET" select="EnumeratedType"/>

          RANGE_TYPE ( UINT8, <xsl:value-of select="$ET"/>,

                     <xsl:apply-templates select="tblEnumValues_WC" mode
="makecopy">
                     <xsl:sort select="EnumValue"/>
                     </xsl:apply-templates>


           </xsl:template>



<xsl:template match="tblEnumValues_WC" mode="makecopy">


<xsl:choose>
<xsl:when test="position() = 1">
                     <xsl:variable name="Start">
                               <xsl:call-template  name="removeSpaces">
                                          <xsl:with-param name="text"
select="EnumValueDescription"/>
                               </xsl:call-template>
                     </xsl:variable>

<xsl:value-of select="$Start"/>

</xsl:when>
<xsl:when test="position() = 3">
 <xsl:variable name="End" >
 <xsl:call-template  name="removeSpaces">
 <xsl:with-param name="text"
          select="EnumValueDescription"/>
 </xsl:call-template>
 </xsl:variable>

, <xsl:value-of select="$End"/>) ;

</xsl:when>
<xsl:otherwise>

</xsl:otherwise>
</xsl:choose>










</xsl:template>



<!--
-->
<!--
-->
<!--
-->


           <xsl:template name="removeSpaces">
                     <xsl:param name="text"/>
                     <xsl:value-of select="translate($text, '&#x20;', ''
)"/>
           </xsl:template>



<!--
-->
<!--
-->
<!--
-->


           <xsl:template name="padSpaces">
                     <xsl:param name="text"/>
                     <xsl:param name="size"/>
                     <xsl:variable name="spaces" select="'
'" />
                     <xsl:value-of select="$text"/>
                     <xsl:choose>
                               <xsl:when test="string-length($text) &lt;
$size">
                                          <xsl:value-of select="substring
($spaces,
1, $size - string-length($text) )"/>
                               </xsl:when>
                     </xsl:choose>
           </xsl:template>

</xsl:stylesheet>





This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. 
If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately.
You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification,
distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.

Visit Us at http://www.polaris.co.in

Current Thread