Re: [xsl] re-ordering nodes/partial sort

Subject: Re: [xsl] re-ordering nodes/partial sort
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Sun, 10 Oct 2004 11:30:37 -0700
Kevin,

Just so you have the info if you need it (and if you can be certain that if the 'supp-desc' element value contains a '(' then it is going to have a 'supp-desc' cousin with the same name minus the descriptor enclosed in '()') then the following will work as far as keeping things in document order and then making copies of all the siblings with the same name and sorting those ascending alphabetically (implied if not specified with the sort attribute)...

If there are other details that will determine whether or not an element should be included or excluded via the first apply-templates then this method won't work but this are not then this is a fairly simple way to accomplish what you want.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:template match="/">
<supplements>
<xsl:apply-templates select="supplements/supp[not(contains(supp-desc, '('))]"/>
</supplements>
</xsl:template>


  <xsl:template match="supp">
    <xsl:for-each select=". | ../supp[contains(supp-desc, current()/supp-desc)]">
    <xsl:sort select="supp-desc"/>
      <xsl:copy-of select="."/>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

Hope this helps!

<M:D/>

Kevin Bird wrote:
Hi

In the following structure I can receive 1 or many <supp> nodes. Some of
the <supp> nodes need re-arranging depending on their <supp-desc>. The
order is: Bed &amp; Breakfast, Bed &amp; Breakfast (child), Half Board,
Half Board (child), Full Board and Full Board (child). Any <supp> with a
<supp-desc> that is not one of the above appear before them in the
output.

Any suggestions would be greatly appreciated.

--INPUT--

<supplements>
	<supp>
		<supp-desc>Sea View</supp-desc>
		<supp-price>10.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Half Board</supp-desc>
		<supp-price>30.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Bed &amp; Breakfast</supp-desc>
		<supp-price>4.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Full Board</supp-desc>
		<supp-price>60.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Half Board (child)</supp-desc>
		<supp-price>15.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Full Board (child)</supp-desc>
		<supp-price>30.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Bed &amp; Breakfast (child)</supp-desc>
		<supp-price>2.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Balcony</supp-desc>
		<supp-price>8.00</supp-price>
	</supp>
</supplements>

--REQUIRED OUTPUT--

<supplements>
	<supp>
		<supp-desc>Sea View</supp-desc>
		<supp-price>10.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Balcony</supp-desc>
		<supp-price>8.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Bed &amp; Breakfast</supp-desc>
		<supp-price>4.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Bed &amp; Breakfast (child)</supp-desc>
		<supp-price>2.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Half Board</supp-desc>
		<supp-price>30.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Half Board (child)</supp-desc>
		<supp-price>15.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Full Board</supp-desc>
		<supp-price>60.00</supp-price>
	</supp>
	<supp>
		<supp-desc>Full Board (child)</supp-desc>
		<supp-price>30.00</supp-price>
	</supp>
</supplements>

Thanks.

--
Kevin

Current Thread