Protiviti / SharePoint Blog

SharePoint Blog

December 19
CAML vs XSLT: Unexpected Query Results When Working with Text

Recently I was doing some work in SharePoint Designer, and I had to move some elements of my CAML query out of CAML and into the XSLT portion of my Data Form Web Part for performance reasons (i.e. working with large lists). In that process, I (re)discovered that there is one important nuance that you must consider.  When doing a query against a text field using CAML - as in the example below, the results will be case insensitive.  Which means my CAML search for "BAT" from the list below will yield results that follow.

Melvin1.PNG

CAML Query #1

melvina.PNG

Melvin2.PNG

CAML Query #2

melvinb.PNGmelvin3.PNG

CAML Query #3

melvinc.PNG

melvin4.PNG

Using the same example list, if I were to use XSLT filtering instead of CAML querying, I would get different results - as shown in the examples below.

XSLT-based XPath Filter #1

melvind.PNG

melvin5.PNG

XSLT-based XPath Filter #2

melvine.PNG

melvin6.PNG

XSLT-based XPath Filter #3

melvinf.PNG
melvin7.PNG

As you can see, the results from the XPATH / XSLT-based filters do not yield the same results due to the case sensitivity effect.  In order to conduct a case insensitive search, with XSLT filtering, you will have to borrow the help an unassuming but quite useful inline function called "transform".  The transform function can be used on strings to force characters to Upper and Lower case as well as remove characters from a string.  For this scenario we will use the transform function to convert both the search term and query results into lowercase so that our filter conditions are applied in a way that returns case insensitive results - just like our CAML queries.

XSLT-based XPath Filter #1 (Revised)

melving.PNG
melvin8.PNG

XSLT-based XPath Filter #2 (Revised)

melvinh.PNG

melvin9.PNG

XSLT-based XPath Filter #3 (Revised)

melvini.PNG
melvin10.PNG

That's all there is to it!  Take some time to explore the utility and versatility of the transform function.  In this example we consistently mapped all the letters of the alphabet to their upper/lowercase counterparts. However, you can target specific characters and map them as you see fit. See the example below where we've mapped uppercase "R" to the lowercase letter "b" to return "Rat" as a valid result of our "Bat" search.

XSLT-based XPath Filter #4

melvinj.PNG
melvin11.PNG

I hope this tip serves you well in your in your SharePoint adventures with CAML, XSLT and Data Form Web parts.




Quick Launch


© Protiviti 2020. All rights reserved.   |   Privacy Policy