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 May 1st, 2006, 04:20 AM
Registered User
Join Date: May 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Check if string is in element


I'm new to this forum and this is my first post so forgive me if i did anything wrong..

I'm suppose to search if a parameter taken in during compilation exists in the LINE elements which I have done so but..How do i return an error message if the search string does not exist in all of my LINE elements in my XML file? I've thought of and tried some approach like setting a variable/parameter to true if found and many others but it doesnt work..Is there anyway I can count the number of LINE elements that consists of the search string?? Hope you can help me out on this! Thanks a lot!!

Old May 1st, 2006, 04:47 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

How are you supposed to return an error message, via the XSLTs normal output or using something like the xsl:message element, which only has limited support in some processors?
Assuming LINE is a child of the context node:
<xsl:variable name=matchingLines" select="LINE[contains(., $searchParam)"/>
  <xsl:when test="$matchingLines">


    <xsl:message terminate="yes">No matching LINE elements found!</xsl:message>

Joe (Microsoft MVP - XML)
Old May 1st, 2006, 04:51 AM
mhkay's Avatar
Wrox Author
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

You can count the lines that contain your parameter like this:

<xsl:param name="searchString">

<xsl:template match="/">
    <xsl:value-of select="count(//LINE[contains(., $searchString)]"/>

If you want the transformation to fail when this count is zero, you can do

<xsl:if test="count(//LINE[contains(., $searchString)] = 0">
  <xsl:message terminate="yes">There are no matching lines</xsl:message>

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Old May 1st, 2006, 09:26 AM
Registered User
Join Date: May 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

Wow! Thanks a lot Joe and Michael! This really helps me a lot! Didnt know there was so much you can do with XSLT and XPath..But I'd like to know what this does:-

//LINE[contains(text(), $search)]

I only know the contains(text(),$search) part.. I thought contains return true or false only?

Thanks a lot for the reply again!

Similar Threads
Thread Thread Starter Forum Replies Last Post
check name of the current node element in xml arunagottimukkala XSLT 19 October 19th, 2007 04:44 AM
Problem -- Empty element check ! back2grave XSLT 2 July 10th, 2006 04:58 PM
change element by string pravind XSLT 1 February 13th, 2006 09:00 AM
check if element is disabled or enabled crmpicco Javascript How-To 1 November 10th, 2005 07:21 PM
check if an array element is not undefined crmpicco Javascript How-To 1 October 6th, 2005 11:40 AM

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