[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: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Tue, 16 Jan 2007 00:06:39 +0100
Re:  Find a specific element or fall back to element 1
John Horner wrote:
<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?

There are many ways that lead to Rome. It can be done very short in XSLT 2, but I guess you are using XSLT 1. Here's one way to tackle it, using only template matching, which will output the number "1" is there is no image with @default, and will print the position of the one with @default when there is one.


<xsl:template match="/">
pos2: <xsl:apply-templates select="$gallery-xml-file/gallery/image" />
</xsl:template>
<xsl:template match="image[@default] | image[not(../image/@default)][1]">
<xsl:value-of select="position()" />
</xsl:template>
<xsl:template match="image" />



Cheers, -- Abel Braaksma http://www.nuntia.nl

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.