Title |
Test
Find
Pattern Title
|
Expression |
^(?<path>(/?(?<step>\w+))+)(?<predicate>\[(?<comparison>\s*(?<lhs>@\w+)\s*(?<operator><=|>=|<>|=|<|>)\s*(?<rhs>('[^']*'|"[^"]*"))\s*(and|or)?)+\])*$ |
Description |
Simple parser of XPath, it doesn't handle the more complex statements but can be of use in some situations. |
Matches |
/root/child1/child2[ @param='value' ] | child1[@param >= "1" and @foo < 'asdf" |
Non-Matches |
/root/*[ local-name() = 'child1' ][ child2/@param='value ]/child2 |
Author |
Rating:
Not yet rated.
Tim Walters
|
Source |
|
Your Rating |
|
Title: XMP Path
Name: Validation
Date: 6/25/2008 6:58:15 AM
Comment:
Very bad regular expression dur to performance point of view
Title: Some nice stuff there...
Name: Darren Neimke
Date: 1/23/2004 4:50:30 AM
Comment:
Tim, I like the pattern as most of it seems quite "tight". Just a couple of things... it would match:
/boo[ @foo <> 'bar' and]
- is that correct? It wouldn't match against this:
/boo[ @foo <> 'bar' and ]
- (note the space after the "and" operator), but, it would probably force quite a few backtrack attempts before failing. I'm not sure how many but, I reckon that there'd be a few. You could also save yourself 3 captured groups by turning on ExplicitCapture (?n) like so:
(?n)
^
(?<path>
(/?
(?<step>\w+)
)+
)
(?<predicate>
\[
(?<comparison>
\s*
(?<lhs>@\w+)
\s*
(?<operator><=|>=|<>|=|<|>)
\s*
(?<rhs>
('[^']*'|"[^"]*")
)
\s*
(and|or)?
)+
\]
)*
$