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

Re: Conditional String Substitution

Subject: Re: Conditional String Substitution
From: Nathan Potter <ndp@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 25 Apr 2008 10:30:12 -0700
Re:  Conditional String Substitution
On Apr 25, 2008, at 10:26 AM, Michael Kay wrote:


Is there any particular reason you have to use XSLT 1.0? This kind of thing
is much easier in 2.0. It more-or-less reduces to


replace(@xlink:href, '\.xml$', '.html')

Well that's handy...


I'll look into how tight my binding is to XSLT 1.0.

I am invoking the transformation inside of a java servlet using the JDOM package. I probably need to look at a different package to use for the transforms...

N





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

-----Original Message-----
From: Nathan Potter [mailto:ndp@xxxxxxxxxxxxxxxxxxxx]
Sent: 25 April 2008 18:23
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Cc: Nathan Potter
Subject:  Conditional String Substitution



Greetings,

I need to do some string manipulation of an attribute value
using XSLT.

I need to look at the value of the attribute "href" and
determine if it ends in the suffix ".xml"

If so then I need to replace the ".xml" suffix with a ".html" suffix.

I got it to work using a pretty convoluted set of XPath
expressions in conjunction with xsl:if but I think that I am
missing something...

Is there a more straight forward way of accomplishing this?


Example(s):


<xsl:template match="thredds:catalogRef">

         <xsl:variable name="href" select="./@xlink:href" />
         <xsl:variable name="linkSuffix" select="substring
($href,string-length($href) - 3)" />
         <xsl:variable name="linkBody" select="substring($href,
1,string-length($href) - 4)" />

         <xsl:if test="$linkSuffix='.xml'">
             <xsl:value-of select="$indent"/><a href="{concat
($linkBody,'.html')}" ><xsl:value-of select="./@xlink:title"/> /</a>
         </xsl:if>

         <xsl:if test="not($linkSuffix='.xml')">
             <xsl:value-of select="$indent"/><a href="{$href}"
<xsl:value-of select="./@xlink:title"/> /</a>
</xsl:if>

</xsl:template>



I used a bunch of variables in the example simply to make it
more readable, the example could be condensed to:

<xsl:template match="thredds:catalogRef">

         <xsl:if test="substring(./@xlink:href,string-length(./
@xlink:href) - 3)='.xml'">
             <a href="{concat(substring(./@xlink:href,1,string-length
(./@xlink:href) - 4),'.html')}" ><xsl:value-of select="./
@xlink:title"/> /</a>
         </xsl:if>

         <xsl:if test="not(substring(./@xlink:href,string-length(./
@xlink:href) - 3))">
             <a href="{./@xlink:href}" ><xsl:value-of
select="./ @xlink:title"/> /</a>
         </xsl:if>
         <br/>

</xsl:template>

Thanks!

Nathan






============================================================ Nathan Potter Oregon State University, COAS ndp at coas.oregonstate.edu 104 Ocean. Admin. Bldg. 541 737 2293 voice Corvallis, OR 97331-5503 541 737 2064 fax


============================================================ Nathan Potter Oregon State University, COAS ndp at coas.oregonstate.edu 104 Ocean. Admin. Bldg. 541 737 2293 voice Corvallis, OR 97331-5503 541 737 2064 fax

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.