Answers
interleaved:
> Rule: Within a leg, departures occur after arrivals, if there are
> arrivals and departures. I see how to validate
that, so long as a
> string compare works. Something like: <xr:validate test="depart >
> arrive" /> I'm not sure how the missing arrival or departure
affects
> that.
Your rule is correct except that,
unfortunately, XPath does not do string comparison; it
tries to convert the values to numbers first. But, there is a workaround.
Modify your rule slightly to remove the non-numeric characters as follows (also
note that I used the ruleset context to enforce the
rule on all legs but the first and last):
<xr:ruleset
context="/schedule/leg[position() > 1 and
position() < last()]">
<xr:validate test="translate(depart,':','') > translate(arrive,':','')" />
</xr:ruleset>
If the arrival or departure is missing,
the rule will fail, which will serve to validate that both nodes exist. If you
want a specific error message when a node is missing, you can add a separate rule
to validate for this condition.
> Rule: For any leg, the arrival is after the previous leg's departue.
For this use the XPath
expression preceding-sibling::leg[1]
to get the preceding sibling. Also, note that I changed the ruleset
context to enforce the rule on all nodes except the first.
<xr:ruleset
context="/schedule/leg[position() > 1]">
<xr:validate test="translate(arrive,':','') > translate(preceding-sibling::leg[1]/depart,':','')"
/>
</xr:ruleset>
> Rule: For the first leg, there is no arrival.
> Rule: For the last leg, there is no departure.
This one is straight forward:
<xr:ruleset
context="/schedule">
<xr:validate test="not(leg[1]/arrive)" />
<xr:validate test="not(leg[last()]/depart)" />
</xr:ruleset>
> Rule: All legs except first and last have both an arrival and a
departure.
This is satisfied by the first ruleset above. However, for an explicit check you can the
following to the first ruleset:
<xr:validate test="boolean(arrive)"
/>
<xr:validate test="boolean(depart)"
/>
> How to do comparisons if the time were expressed in a manner not
> easily compared as strings, e.g. "
Not easy. XPath doesn’t
recognize a date/time type. I’d recommend using the standard XML format
for dates: 2006-01-30T08:00. This is the ‘standard’ way for dates
in XML, and with it you can use the translate() trick
to remove the -, T, and : and perform date comparisons.
So, your complete XRules document will
look like this:
<xr:rules xmlns:xr="http://www.xrules.org/2003/11">
<xr:ruleset
context="/schedule/leg[position() > 1 and
position() < last()]">
<xr:validate test="translate(depart,':','') > translate(arrive,':','')" />
<xr:validate test="boolean(arrive)"
/>
<xr:validate test="boolean(depart)"
/>
</xr:ruleset>
<xr:ruleset
context="/schedule/leg[position() > 1]">
<xr:validate test="translate(arrive,':','') > translate(preceding-sibling::leg[1]/depart,':','')"
/>
</xr:ruleset>
<xr:ruleset
context="/schedule">
<xr:validate test="not(leg[1]/arrive)" />
<xr:validate test="not(leg[last()]/depart)" />
</xr:ruleset>
</xr:rules>
Regards,
Waleed
-----Original Message-----
From: xrules@yahoogroups.com
[mailto:xrules@yahoogroups.com] On Behalf Of johnbrockus
Sent:
To: xrules@yahoogroups.com
Subject: [xrules] How to do
sibling checks? non-string compares?
Imagine a truck delivery schedule. Suppose xml is:
...
<schedule>
<leg>
<depart>
</leg>
<leg>
<arrive>
<depart>
</leg>
<leg>
<arrive>
<depart>
</leg>
<leg>
<arrive>
</leg>
</schedule>
...
Rule: Within a leg, departures occur after arrivals, if there are
arrivals and departures. I see how to validate that, so long as a
string compare works. Something like: <xr:validate test="depart >
arrive" /> I'm not sure how the missing arrival or departure affects
that.
Rule: For any leg, the arrival is after the previous leg's departue.
Rule: For the first leg, there is no arrival.
Rule: For the last leg, there is no departure.
Rule: All legs except first and last have both an arrival and a departure.
2 questions:
How is best to express these rules?
How to do comparisons if the time were expressed in a manner not
easily compared as strings, e.g. "
Thanks.