Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Getting the parent when the child doesn't exist [Thread Next] Re: Getting the parent when the child doesn't existTo: NULL Date: 3/3/2009 1:32:00 PM On Mar 3, 9:50=A0am, "Greg Collins" <gcollins_at_msn_dot_com> wrote: > I'm posting this to both the XSL and C# newsgroups because it spans both. > > I have an XPath passed to me that starts at the root node and goes down t= o a > table row node. From this XPath I was grabbing the table node by appendin= g a > "/.." to the end of the XPath. > > While this technique works great when the row exists, if the row does not > exist, I can no longer identify the table node. > > In code, one might say, well just rip off everything past the last slash > character. This won't work because I cannot guarantee that a filter is no= t > being used. And there might be nested filters and "[" and "]" characters > used in the filter as values. > > So what I need to find is a way to extrapolate the parent XPath from that > which is passed in, but accounting for potential nested filters and [ ] > characters used as filter values. As Jeroen says, there is no truly generic solution, as an XPath expression needs not explicitly mention the parent of the child at all. If you can guarantee that the expression truly has the form /foo/bar/ baz (i.e. only ever uses child:: axis), and optional filters on every step, then you can of course just parse that expression and drop the final part of the path. You might even be able to handle that sort of parsing with regex, as .NET ones are powerful enough to handle nested square brackets - for an example, see http://www.m-8.dk/resources/RegEx-nes= ted-constructions.aspx. Even so, it may be very tricky to get right, and quite likely it is better to just write a proper XPath parser (only handling the simplified grammar as described earlier - this will also ensure that, if something wrong is passed to you, you don't silently parse it wrong). XPath grammar is fairly simple and unambiguous, so you shouldn't have a problem a parser generator - COCO/R is neat and very small, ANTLR is another good and very powerful one, and there are plenty others. | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
