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

RE: Find a specific element or fall back to element 1

Subject: RE: Find a specific element or fall back to element 1
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 15 Jan 2007 23:05:46 -0000
RE:  Find a specific element or fall back to element 1
Try

select="(image[1] | image[@default='true'])[last()]"

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

> -----Original Message-----
> From: John Horner [mailto:Horner.John@xxxxxxxxxx] 
> Sent: 15 January 2007 22:50
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  Find a specific element or fall back to element 1
> 
> I have a gallery.xml file containing a number of images:
> 
> <gallery>
>     <image>
>       <filename>1.jpg</filename>
>     </image>
>     <image>
>       <filename>2.jpg</filename>
>     </image>
>     <!-- etc -->
>     <image default="true">
>       <!-- optional attribute applies to this element -->
>       <filename>10.jpg</filename>
>     </image>
> </gallery>
> 
> and my xslt has to do the following thing -- look for an 
> image marked with a "default" attribute, and if it's not 
> found, use the first image instead. So I'm looking for the 
> position number of the marked element, and if I can't find 
> it, using 1:
> 
> <xsl:variable name="gallery-xml-file"
> select="document($gallery-xml-path)"/>
> <xsl:variable name="test-position">
>   <xsl:for-each select="$gallery-xml-file//image">
>     <xsl:if test="@default='true'">
>       <xsl:value-of select="position()"/>
>     </xsl:if>
>   </xsl:for-each>
> </xsl:variable>
> <xsl:variable name="display-position">
>   <xsl:choose>
>     <xsl:when test="$test-position = ''">1</xsl:when>
>     <xsl:otherwise>
>       <xsl:value-of select="$test-position"/>
>     </xsl:otherwise>
>   </xsl:choose>
> </xsl:variable>
> 
> This seems a bit long and wasteful to me. Is there a better way?
> 
> ==============================================================
> ================
> The information contained in this email and any attachment is 
> confidential and
> may contain legally privileged or copyright material.   It is 
> intended only for
> the use of the addressee(s).  If you are not the intended 
> recipient of this email, you are not permitted to 
> disseminate, distribute or copy this email or any 
> attachments.  If you have received this message in error, 
> please notify the sender immediately and delete this email 
> from your system.  The ABC does not
> represent or warrant that this transmission is secure or 
> virus free.   Before
> opening any attachment you should check for viruses.  The 
> ABC's liability is limited to resupplying any email and 
> attachments 
> ==============================================================
> ================

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.