RE: [xsl] sorting (again)

Subject: RE: [xsl] sorting (again)
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 29 Nov 2007 17:38:43 -0000
Make your first (primary) sort key be a boolean value - false if this is the
DictionaryModelDescriptor node, true if it is not. 

In XSLT 2.0 if <xsl:sort select="..."> returns a boolean, it will sort false
before true. XSLT 1.0 will convert the boolean to a string - so "false"
still comes before "true". 

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Davis Ford [mailto:davisford@xxxxxxxxx] 
> Sent: 29 November 2007 17:10
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] sorting (again)
> 
> Hi, one more sorting question.
> 
> Given the XML pasted below...I'm using the following 
> transform to sort it canonically.  However, I need the 
> "DictionaryModelDescriptor" node to always be the first node, 
> and then the rest are sorted canonically.
>  I tried this, but it does not do what I want.
> 
> 	<xsl:template match="@*|node()">
>   		<xsl:copy>
>     			<xsl:apply-templates select="@*|node()">
>                                 <xsl:sort 
> select="DictionaryModelDescriptor"/>
> 				<xsl:sort select="@typeName"/>
>       				<xsl:sort select="name(.)"/>
>       				<xsl:sort />
>     			</xsl:apply-templates>
>   		</xsl:copy>
> 	</xsl:template>
> 
> This one puts it at the top, but now I have a duplicate in the middle
> 
> 	<xsl:template match="@*|node()">
>   		<xsl:copy>
>                         <xsl:apply-templates 
> select="DictionaryModelDescriptor"/>
>     			<xsl:apply-templates select="@*|node()">
> 				<xsl:sort select="@typeName"/>
>       				<xsl:sort select="name(.)"/>
>       				<xsl:sort />
>     			</xsl:apply-templates>
>   		</xsl:copy>
> 	</xsl:template>
> 
> How can I "move" the DictionaryModelDescriptor node to the 
> top after sorting?
> 
> Thanks in advance,
> Davis
> 
> XML ->
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <DictionaryModel>
>     <DictionaryModelDescriptor about="modelName1/modelVersion1">
>         <modelName>modelName1</modelName>
>         <modelVersion>modelVersion1</modelVersion>
>     </DictionaryModelDescriptor>
>     <DictionaryParts>
>         <DictionaryPart abstract="false" typeName="part.full1">
>             <children/>
>             <parents/>
>             <DictionaryProperties/>
>             <feature>feature.empty3</feature>
>             <maxCardinality>2</maxCardinality>
>             <minCardinality>1</minCardinality>
>             <parentComposition>feature.empty2</parentComposition>
>             <role>role</role>
>             <sequence>1</sequence>
>         </DictionaryPart>
>         <DictionaryPart abstract="false" typeName="part.full5">
>             <children/>
>             <parents/>
>             <DictionaryProperties/>
>             <feature>feature.empty13</feature>
>             <maxCardinality>2</maxCardinality>
>             <minCardinality>1</minCardinality>
>             <parentComposition>feature.empty12</parentComposition>
>             <role>role</role>
>             <sequence>1</sequence>
>         </DictionaryPart>
>     </DictionaryParts>
>     <DictionaryAssociations>
>         <DictionaryAssociation abstract="false" 
> typeName="association.empty1">
>             <children/>
>             <parents/>
>             <DictionaryProperties/>
>         </DictionaryAssociation>
>         <DictionaryAssociation
> compositionType="BIDIRECTIONAL_ASSOCIATION" abstract="false"
> typeName="association.full2">
>             <children/>
>             <parents/>
>             <DictionaryProperties>
>                 
> <DictionaryProperty>simple.property.empty4</DictionaryProperty>
> 
> <DictionaryProperty>composite.property.empty4</DictionaryProperty>
>             </DictionaryProperties>
>             <sourcePart>part.full2</sourcePart>
>             <targetPart>part.full3</targetPart>
>             <resolverStrategy>resolver.strategy</resolverStrategy>
>         </DictionaryAssociation>
>     </DictionaryAssociations>
>     <DictionaryFeatures>
>         <DictionaryFeature composite="false" abstract="false"
> typeName="feature.empty1">
>             <children/>
>             <parents/>
>             <DictionaryProperties/>
>             <associations/>
>             <parentComposites/>
>             <parts/>
>         </DictionaryFeature>
>         <DictionaryFeature composite="true" compositionType="SEQUENCE"
> abstract="true" typeName="feature.full8">
>             <children>
>                 <child>feature.empty9</child>
>             </children>
>             <parents/>
>             <DictionaryProperties>
> 
> <DictionaryProperty>composite.property.empty5</DictionaryProperty>
>                 
> <DictionaryProperty>simple.property.empty5</DictionaryProperty>
>             </DictionaryProperties>
>             <associations>
>                 <association>association.empty3</association>
>                 <association>association.empty4</association>
>             </associations>
>             <parentComposites>
>                 <parentComposite>feature.empty10</parentComposite>
>             </parentComposites>
>             <parts>
>                 <part>part.empty4</part>
>             </parts>
>             <code>code</code>
>         </DictionaryFeature>
>     </DictionaryFeatures>
>     <DictionaryPropertyNodes>
>         <DictionaryPropertyNode abstract="false"
> typeName="property.node.empty1">
>             <children/>
>             <parents/>
>             <DictionaryProperties/>
>         </DictionaryPropertyNode>
>         <DictionaryPropertyNode abstract="false" 
> typeName="property.node.full3">
>             <children>
>                 <child>property.node.empty4</child>
>             </children>
>             <parents>
>                 <parent>property.node.empty5</parent>
>             </parents>
>             <DictionaryProperties>
> 
> <DictionaryProperty>composite.property.empty11</DictionaryProperty>
>                 
> <DictionaryProperty>simple.property.empty8</DictionaryProperty>
>             </DictionaryProperties>
>         </DictionaryPropertyNode>
>     </DictionaryPropertyNodes>
>     <DictionaryProperties>
>         <DictionaryProperty
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> xsi:type="CompositeDictionaryProperty" propertyType="COMPOSITE_ROOT"
> inherited="false" typeName="composite.property.empty1">
>             <maxCardinality>2147483647</maxCardinality>
>             <minCardinality>-2147483648</minCardinality>
>             <children/>
>         </DictionaryProperty>
>         <DictionaryProperty
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> xsi:type="CompositeDictionaryProperty" propertyType="COMPOSITE_ROOT"
> inherited="false" typeName="composite.property.empty2">
>             <maxCardinality>2147483647</maxCardinality>
>             <minCardinality>-2147483648</minCardinality>
>             <children/>
>         </DictionaryProperty>
>         <DictionaryProperty
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> xsi:type="CompositeDictionaryProperty" propertyType="COMPOSITE_ROOT"
> inherited="false" typeName="composite.property.empty3">
>             <maxCardinality>2147483647</maxCardinality>
>             <minCardinality>-2147483648</minCardinality>
>             <children/>
>         </DictionaryProperty>
>         <DictionaryProperty
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> xsi:type="CompositeDictionaryProperty" propertyType="COMPOSITE_CHILD"
> inherited="true" typeName="composite.property.full6">
>             <code>code</code>
>             <maxCardinality>3</maxCardinality>
>             <minCardinality>2</minCardinality>
>             <parentObject>feature.empty14</parentObject>
>             <parentProperty>composite.property.empty8</parentProperty>
>             <rootProperty>composite.property.empty7</rootProperty>
>             <children>
>                 <child>simple.property.empty6</child>
>             </children>
>             <propertyNode>property.node.empty2</propertyNode>
>         </DictionaryProperty>
>         <DictionaryProperty
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> xsi:type="SimpleDictionaryProperty" propertyType="SIMPLE_ROOT"
> inherited="false" typeName="simple.property.empty1">
>             <maxCardinality>2147483647</maxCardinality>
>             <minCardinality>-2147483648</minCardinality>
>         </DictionaryProperty>
>         <DictionaryProperty
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> xsi:type="SimpleDictionaryProperty" propertyType="SIMPLE_ROOT"
> inherited="false" typeName="simple.property.empty2">
>             <maxCardinality>2147483647</maxCardinality>
>             <minCardinality>-2147483648</minCardinality>
>         </DictionaryProperty>
>         <DictionaryProperty
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> xsi:type="SimpleDictionaryProperty" propertyType="SIMPLE_ROOT"
> inherited="false" typeName="simple.property.empty3">
>             <maxCardinality>2147483647</maxCardinality>
>             <minCardinality>-2147483648</minCardinality>
>         </DictionaryProperty>
>         <DictionaryProperty
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> xsi:type="SimpleDictionaryProperty" propertyType="SIMPLE_CHILD"
> inherited="true" typeName="simple.property.full7">
>             <code>code</code>
>             <maxCardinality>2</maxCardinality>
>             <minCardinality>1</minCardinality>
>             <parentObject>feature.empty16</parentObject>
>             <parentProperty>composite.property.empty9</parentProperty>
>             <rootProperty>composite.property.empty10</rootProperty>
>             <defaultValue>default-value</defaultValue>
>             
> <dictionaryFeatureRange>feature.range2</dictionaryFeatureRange>
>             <referentFeature>feature.empty17</referentFeature>
>             <dictionaryValueRange>value.range1</dictionaryValueRange>
>             <valueTypeName>value-type-name</valueTypeName>
>         </DictionaryProperty>
>     </DictionaryProperties>
>     <DictionaryFeatureRanges>
>         <DictionaryFeatureRange typeName="feature.range1">
>             <DictionaryFeatures>
>                 <DictionaryFeature>feature.empty11</DictionaryFeature>
>             </DictionaryFeatures>
>         </DictionaryFeatureRange>
>     </DictionaryFeatureRanges>
>     <DictionaryValueRanges>
>         <DictionaryValueRange typeName="value.range2">
>             <DictionaryRangeItems>
>                 <DictionaryRangeItem>range.item3</DictionaryRangeItem>
>             </DictionaryRangeItems>
>             <valueTypeName>value-type-name</valueTypeName>
>         </DictionaryValueRange>
>     </DictionaryValueRanges>
>     <DictionaryRangeItems>
>         <DictionaryRangeItem typeName="range.item2">
>             <value>4</value>
>         </DictionaryRangeItem>
>     </DictionaryRangeItems>
>     <DictionaryValueTypeNames>
>         <DictionaryValueTypeName>a value type 
> name</DictionaryValueTypeName>
>         
> <DictionaryValueTypeName>value-type-name</DictionaryValueTypeName>
>     </DictionaryValueTypeNames>
> </DictionaryModel>

Current Thread