xsl-list
[Top] [All Lists]

Re: [xsl] Using contains with more than one string - XSLT2.0

2015-01-28 14:35:42
If you're using XSLT 2, the function you want is matches() rather than contains(). That allows you to use regular-expression syntax to express a choice of strings to match. In this case you want

<xsl:for-each select="//dsc//unittitle[matches(normalize-space(.),'Photo|Neg')]

or you can put the regular expression in your $target variable, so that simply replacing "contains" with "matches" in your code below will work.

matches() is a lot more powerful than contains() because of its support for regular expressions and case insensitivity (among other things).

David S.

 On Wed, 28 Jan 2015, Michele R Combs mrrothen(_at_)syr(_dot_)edu wrote:

I've done some googling on this but nothing seems quite to match what I want to 
do.

At the top of my style sheet I have this:

  <xsl:variable name="target" select="'Photo'"/>

Then later I do this to find all titles with that string in them:

  <xsl:for-each select="//dsc//unittitle[contains(normalize-space(.),$target)]">

So far so good, works great.  The problem comes when I want to tell the 
contains function to match on any one of several strings, e.g. Photo or Neg or 
Film.  So far I have tried the following but none of them work:

  <xsl:variable name="target" select="'Photo|Neg'"/>
  <xsl:variable name="target" select="'Photo'|'Neg'"/>
  <xsl:variable name="target" select="'Photo' or 'Neg'"/>
  <xsl:variable name="target" select="'Photo Neg'"/>

If I must, I can stick the or'ed terms directly in the contains function rather 
than using a variable, but when I tried a few variations on that, they didn't 
work either.

Thanks

Michele
+++++++++++++++
Michele Combs
Lead Archivist
Special Collections Research Center
Syracuse University Libraries
315-443-2081
mrrothen(_at_)syr(_dot_)edu
scrc.syr.edu



--
David Sewell, Editorial and Technical Manager
ROTUNDA, The University of Virginia Press
PO Box 400314, Charlottesville, VA 22904-4314 USA
Email: dsewell(_at_)virginia(_dot_)edu   Tel: +1 434 924 9973
Web: http://rotunda.upress.virginia.edu/
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>