[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: XSLT to find missing characters?

Subject: RE: XSLT to find missing characters?
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Fri, 12 Jul 2002 18:13:06 +0100
xslt find
Sort the search letter as a marker into the sequence of beers:

<xsl:variable name="x">
<xsl:copy-of select="beers"/>
<marker name="{$searchLetter}"/>
</xsl:variable>

<xsl:variable name="y">
<xsl:for-each select="exslt:node-set($x)/*">
  <xsl:sort select="@name"/>
  <xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>

Then find the next beer after the marker:

<xsl:value-of select="exslt:node-set($y)/
     marker/following-sibling::beer/@name"/>

This doesn't require the beers to be presorted. 

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx 

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of 
> Kirk Allen Evans
> Sent: 12 July 2002 13:09
> To: Xsl-List@Lists. Mulberrytech. Com
> Subject:  XSLT to find missing characters?
> 
> 
> Was hoping for a few hundred extra pairs of eyes for this, 
> maybe another solution.  I need to find the first element in 
> a node set that matches a criteria.  If that node is not 
> present in the node set, grab the next node in the node set 
> alphabetically.  Here's the example:
> 
> <beers>
> 	<beer name="Amstel Light"/>
> 	<beer name="Budweiser"/>
> 	<beer name="Bud Lite"/>
> 	<beer name="Coors"/>
> 	<beer name="Michelob"/>
> 	<beer name="Miller Lite"/>
> 	<beer name="Sam Adams"/>
> </beers>
> 
> If "B" is sent as the parameter, "Budweiser" is output.  If 
> "D" is sent, then "Michelob" is output.  If no matches are 
> found, the first element is used for the output.  Here is my 
> working stylesheet.  I was wondering if anyone could think of 
> a different approach to solving this problem, perhaps with 
> fewer string manipulations?
> 
> 
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> 	<xsl:param name="searchLetter" select="'D'"/>
> 	<xsl:variable name="alphabet" 
> select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
> 
> 	<xsl:output method="text"/>
> 	<xsl:template match="/">
> 		<xsl:variable name="beerName">
> 			<xsl:call-template name="CheckAlphabet">
> 				<xsl:with-param name="charPos"
> select="string-length(substring-before($alphabet,$searchLetter
> )) + 1"/>
> 			</xsl:call-template>
> 		</xsl:variable>
> 		<xsl:value-of select="$beerName"/>
> 	</xsl:template>
> 
> 	<xsl:template name="CheckAlphabet">
> 		<xsl:param name="charPos" select="1" />
> 		<xsl:variable name="alphabetLength" select="26" />
> 		<xsl:choose>
> 			<xsl:when test="$charPos &lt;= $alphabetLength">
> 				<xsl:choose>
> 					<xsl:when 
> test="/beers/beer[starts-with(@name,substring($alphabet,$charP
> os,1))][1]">
> 						<xsl:value-of 
> select="/beers/beer[starts-with(@name,substring($alphabet,$cha
> rPos,1))][1]/@
> name"/>
> 					</xsl:when>
> 					<xsl:otherwise>
> 						
> <xsl:call-template name="CheckAlphabet">
> 							
> <xsl:with-param name="charPos" select="$charPos + 1"/>
> 						</xsl:call-template>
> 					</xsl:otherwise>
> 				</xsl:choose>
> 			</xsl:when>
> 			<xsl:otherwise>
> 				<xsl:value-of 
> select="/beers/beer[1]/@name"/>
> 			</xsl:otherwise>
> 		</xsl:choose>
> 	</xsl:template>
> </xsl:stylesheet>
> 
> Kirk Allen Evans
> http://www.xmlandasp.net
> "XML and ASP.NET", New Riders Publishing 
> http://www.amazon.com/exec/obidos/ASIN/0735712> 00X
> 
> 
>  XSL-List 
> info and archive:  
> http://www.mulberrytech.com/xsl/xsl-list
> 
> 


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.