XML queries with case insensitive tag

Posted on

Question :

I have a requirement where i want XML tags to be case insensitive in my xml queries.

For example:

DECLARE @myTable TABLE ( yourXML XML )  

INSERT INTO @myTable SELECT '<z><a><b>1</b><c>2</c></a></z>'
INSERT INTO @myTable SELECT '<Z><A><b>1</b><c>2</c></A></Z>'   

SELECT * FROM @myTable WHERE ( [yourXML].exist('for $x in /z/a where  ( ($x/b[1]) = 1 )  return $x')>0 ) 

Returns output as


But i want the output to display without considering case of the XML tag. So my desired output should be


How it can be done?

Answer :

Use predicates with name tests like this instead of “simple” node steps:

/*[lower-case(local-name(.)) = 'z']/*[lower-case(local-name(.)) = 'a']

Somewhat simpler than the FLWOR version.

select *
from myTable
where yourXML.exist('(Z/A,z/a)[(b,B)=1]') = 1

Leave a Reply

Your email address will not be published. Required fields are marked *