Tuesday, 2 February 2010

XPath 1.0 Query and ends-with

I was trying to find a set of nodes in an xml document that ended in "id" (personid, addressid etc). Upon discovering that version 1.0 of XPath does not support the ends-with expression that is supported in version 2.0 it was time for a work around!

After much research on the web I found that the only real way to do this was to use the substring function thus:

xmldoc.SelectNodes("//*[substring(name(), string-length(name()) - 1) = 'id']");

This delivers back a node set that was exactly what I wanted!

No comments:

Post a Comment