Using string-compare in an xpath expression for XmlNode.SelectNodes

I have an XmlNode I want to query (actually data returned from SharePoint services' GetListCollection method).

The top-level Node is 'Lists' and it Contains many 'List's, each 'List' has an attribute 'ID' which is a guid. I can’t be sure the cases of the two guids will match so I want t do a case insensitive match. Can anyone shed some light on how to do this

Currently I am doing this:

XmlNode listCollection =webservice.GetListCollection();

XmlDocument xmlDoc = new XmlDocument();

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

nsmgr.AddNamespace("item", "http://schemas.microsoft.com/sharepoint/soap/");

nsmgr.AddNamespace("ms", "xmlns:ms='urn:schemas-microsoft-com:xslt'");

string query = string.Format("//item:List[ms:string-compare(@ID, '{0}', 'en-US', 'i')=0]", m_parameters["guid"]);

XmlNodeList lists = listCollection.SelectNodes(query, nsmgr);

The last line throws an exception:

[System.Xml.XPath.XPathException]: {"XsltContext is needed for this query because of an unknown function."}

The following works:

string query = string.Format("//item:List[@ID='{0}']", m_parameters["guid"].ToUpper());

… but I’m not entirely happy with it. I’d be much happier with a case-insensitive search.



Answer this question

Using string-compare in an xpath expression for XmlNode.SelectNodes

  • Senthil.P

    An extension function is not necessary to perform a case-insensitive string equality comparison (assuming a limited alphabet as is the case with GUIDs)

    An example of an XPath expression selecting all children of the current node , such that their ID attribute is case-insesitively equal to a given string $someString:

    someElem[translate(@ID, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' )
    =
    translate($someString, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' )
    ]

    Thanks,
    Dimitre Novatchev


  • barbbayne

    In XSLT you can use scripts.

    In XPath you can implements your own XsltContext and pass it to XPathExpression.SetContext() method.

    This XstlContext is called each time XPath meats unknown function and this is the way to extend XPath with custom functions and variables.



  • Roozbeh Sharafi

    Thanks for the quick response Dimitre.

    If I do want to use the string-compare function what would I need to do

    (obviously hypothetical as your suggestion of using translate works)


  • Using string-compare in an xpath expression for XmlNode.SelectNodes