Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 5th, 2006, 02:34 PM
Registered User
 
Join Date: May 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parsing string to check for substring

I'm just starting my foray into the xslt world so bare with me :) I have the following xml...

Code:
<channel>
   <title>parent channel title</title>
   <id>parent id</id>
   <channel>
      <title>child channel title</title>
      <id>child id</id>
   </channel>
   <channel>
      <title>another child channel title></title>
      ...etc.
</channel>
Each parent channel has two sub channels. The titles of the subchannels will contain (but aren't limited to) the strings "Highlights" or "Features". I need to parse the title, check for the existence of either of the two substrings and then take action based on the match. So, in laymans terms i want to do this...

Code:
for-each "sub-channel"
   if "sub-channel" title contains string "highlights"
      <a href="page.php?view=highlights">highlights link</a>
   else if "sub-channel" title contains string "features"
      <a href="page.php?view=features">features link</a>
   /if
/for-each
any tips for a newbie? feel free to dumb down your responses. i'm new to XSLT and scripting/programming in general.

thanks!
 
Old June 5th, 2006, 02:59 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Assuming you're positioned at the parent channel element (this is very important), you can do:

<xsl:for-each select="channel">
  <xsl:choose>
    <xsl:when test="contains(title, 'highlights')">
      <a href="page.php?view=highlights">highlights link</a>
    </xsl:when>
    <xsl:when test="contains(title, 'features')">
      <a href="page.php?view=features">features link</a>
    </xsl:when>
    <xsl:otherwise>
      (optional)
    </xsl:otherwise>
  </xsl:choose>
</xsl:for-each>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 5th, 2006, 06:44 PM
Registered User
 
Join Date: May 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again Michael!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing of String jmss66 VB How-To 9 May 1st, 2008 12:47 AM
parsing a string using charindex and substring cole SQL Server 2000 3 March 20th, 2007 02:09 AM
Parsing a String? MBowen SQL Server 2000 9 December 1st, 2006 01:17 PM
How to extract a substring from a string.... muralish MySQL 2 May 18th, 2005 06:58 AM
SQL String Parsing TdyIndy SQL Language 2 July 26th, 2004 10:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.