Checking regex with replace and URI test
We tried to figure out how to test a valid date pattern where also a year is valid. Herefore we needed a "only ONE of the patterns must be true" construction but when we use the OR in the regexp
the "match" function acts more likely as a find function. Ergo an OR returns true if ONE or MORE expressions is true.
This is the Date check regex (remove the LineBreaks I put that in for readability):
((P)(((([1-9])(\d)*)(J)){{0,1}}((([1-9])(\d)*)(M)){{0,1}}((([1-9])
(\d)*)(D)){{0,1}}))(((T(([1-9])(\d)*)(H))((([1-9])(\d)*)(M)){{0,1}}
((([1-9])(\d)*(\.((\d)*([1-9])(\d)*)){{0,1}})(S)){{0,1}})|((T(([1-9])
(\d)*)(M))((([1-9])(\d)*(\.((\d)*([1-9])(\d)*)){{0,1}})(S)){{0,1}})
|((T(([1-9])(\d)*(\.((\d)*([1-9])(\d)*)){{0,1}})(S)){{0,1}}))
We check the date but also only a year like 2004 is valid. So if I enter 2004-20-18 (which is no valid date) returns true when I use the match function, because the first part (2004) is a valid expression. But when I use the "replace" function it works OK. Because if a specific part of the OR does not match the function will return nothing. If more parts are true it returns more replace characters.
test = "replace(./datetime,'((P)(((([1-9])(\d)*)(J)){{0,1}}((([1-9])(\d)*)(M))
{{0,1}}((([1-9])(\d)*)(D)){{0,1}}))(((T(([1-9])(\d)*)(H))((([1-9])(\d)*)
(M)){{0,1}}((([1-9])(\d)*(\.((\d)*([1-9])(\d)*)){{0,1}})(S)){{0,1}})
|((T(([1-9])(\d)*)(M))((([1-9])(\d)*(\.((\d)*([1-9])(\d)*)){{0,1}})(S)){{0,1}})|
((T(([1-9])(\d)*(\.((\d)*([1-9])(\d)*)){{0,1}})(S)){{0,1}}))','x') = 'x'"
So if I check "A or B" and it turns out that A and B are true the function returns "xx" but when you want to check if there is only but one true I check if the return value is equal to "x".
We also have made a regular expression for a URI it was huge...and could not be used in the xslt because of the many "&" characters in it. We tried the "instance of" construction but that also did not work.
But thanx for the WROX book "XPATH2.0 programmers reference" that explains everything I needed, the construction:
test="data('http:www.wrox.com') castable as xsd:anyURI" works fine to test a valid URI. (or do I reveal now some secrets from the book)
so thanx for that.
|