xsl-list
[Top] [All Lists]

[xsl] Coditional Sort By

2007-05-24 02:32:05
Hi, I have a XML file like this:

<records>
   <record>
      <field id="130">a field value</field>
      <field id="245">ab field value</field>
      ...
   </record>
   <record>
      <field id="130">ab field value</field>
      <field id="245">ac field value</field>
      ...
   </record>
   <record>
      <field id="245">aa field value</field>
      ...
   </record>
   ...
</records>

I want to sort the record elements using either the field[(_at_)id='130'] or field[(_at_)id='245'] as sorting key, if one is present. The one with the '130' takes precedence over the one with '245'. If a record has a field[(_at_)id='130'] use it as the sort key if there is no field[(_at_)id='130'] then use the field[(_at_)id='245']; so that the resulting XML would be:

<records>
   <record>
      <field id="130">a field value</field>
      <field id="245">ab field value</field>
      ...
   </record>
   <record>
      <field id="245">aa field value</field>
      ...
   </record>
   <record>
      <field id="130">ab field value</field>
      <field id="245">ac field value</field>
      ...
   </record>
   ...
</records>

Is this possible? I'm thinking of using a variable to hold the possible sort keys of all records then use it as the sort key in my xsl:sort instruction. Something like:

<xsl:variable name="sort.key" as="xs:string+" select="for $i in (/records/record) return if ($i/field[(_at_)id='130']) then $i/field[(_at_)id='130'] else $i/field[(_at_)id='245']"/>

<xsl:template match="records">
   <xsl:apply-templates select="record">
      <xsl:sort select="$sort.key"/>
   </xsl:apply-templates>
</xsl:template>

Thanks,

Jeff


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