xsl-list
[Top] [All Lists]

Sort Question

2005-06-01 14:22:20

Hi

Sorry this is so long, I am trying to sort the enumerated values of a
type,
To generate C code from an XML document.

Given the following XML file...

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:noNamespaceSchemaLocation="irs.xsd" generated="2005-06-01T11:26:34">
<tblEnumeratedTypes_WC>
        <EnumeratedType>ColorType</EnumeratedType>
        <tblEnumValues_WC>
                <EnumeratedType>ColorType</EnumeratedType>
                <EnumValue>1</EnumValue>
                <EnumValueDescription>Red</EnumValueDescription>
        </tblEnumValues_WC>
        <tblEnumValues_WC>
                <EnumeratedType>ColorType</EnumeratedType>
                <EnumValue>8</EnumValue>
                <EnumValueDescription>White</EnumValueDescription>
        </tblEnumValues_WC>
        <tblEnumValues_WC>
                <EnumeratedType>ColorType</EnumeratedType>
                <EnumValue>3</EnumValue>
                <EnumValueDescription>Blue</EnumValueDescription>
        </tblEnumValues_WC>
</tblEnumeratedTypes_WC>
</dataroot>

I get these retults :



        
//----------------------------------------------------------------------
----------------
        
//----------------------------------------------------------------------
----------------
        //      Enumerated Types
        
//----------------------------------------------------------------------
----------------
        
//----------------------------------------------------------------------
----------------
        
        
//----------------------------------------------------------------------
----------------
        // Name         : ColorType
        // Description    :                                             
        
//----------------------------------------------------------------------
----------------
        

        enum {
                        ColorTypeRed                  = 1, 
                Blue                 = 3, 
                White                = 8
        }   ;
         
        RANGE_TYPE ( UINT8, ColorType, Red, Blue ) ;

XSL File used is at the end of this document.
        
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).

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

To make it use the sorted list. 

Thanks in Advance, 
Joe Simon



After Applying the following XSL


<?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";>
        <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>
                                <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="Start">
                        <xsl:call-template  name="removeSpaces">
                                <xsl:with-param name="text"
select="(.//EnumValueDescription)[1]"/>
                        </xsl:call-template>
                </xsl:variable>
                        <xsl:variable name="End" >
                                <xsl:call-template  name="removeSpaces">
                                        <xsl:with-param name="text"
select="(.//EnumValueDescription)[last()]"/>
                                </xsl:call-template>
                        </xsl:variable>
        <xsl:variable name="ET" select="EnumeratedType"/> 
        RANGE_TYPE ( UINT8, <xsl:value-of select="$ET"/>, <xsl:value-of
select="$Start"/>, <xsl:value-of select="$End"/> ) ;

        </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 space Intentionally Left Blank 

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



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