Reputation:
Hi, this is the content of my XML file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<mainNode>
<sub time="08:00">
<status id="2">On</status>
<status id="3">Off</status>
</sub>
<sub time="13:00">
<status id="4">On</status>
<status id="7">On</status>
</sub>
<sub time="16:00">
<status id="5">On</status>
<status id="6">On</status>
<status id="7">Off</status>
<status id="8">On</status>
</sub>
<sub time="20:00">
<status id="4">Off</status>
<status id="7">On</status>
</sub>
<sub time="23:59">
<status id="4">On</status>
<status id="7">On</status>
</sub>
</mainNode>
My program gets the current time: if I get 15.59, I must retrieve all the status id of the next sub time (16.00):
<sub time="16:00">
<status id="5">On</status>
<status id="6">On</status>
<status id="7">Off</status>
<status id="8">On</status>
</sub>
With a second XPath query I must get all the status id of the previous sub time (13.00). How to do it? I know SQL but I'm quite new to XPath. I accept urls to serious XPath resources too, if any. Thanks!
Upvotes: 2
Views: 3403
Reputation: 243449
Here are two solutions:
This is one pair of XPath 1.0 expressions that select the required nodes:
/*/*
[translate(@time, ':','')
>
translate('15:59',':','')
][1]
selects the first sub
node with time later than 15:59
.
/*/*
[translate(@time, ':','')
<
translate('15:59',':','')
][last()]
selects selects the first sub
node with the previous than 15:59
sub
time.
We can include these in an XSLT transformation and check that the really wanted result is produced:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
First time after 15:59:
<xsl:copy-of select=
"/*/*
[translate(@time, ':','')
>
translate('15:59',':','')
][1]
"/>
First time before 15:59:
<xsl:copy-of select=
"/*/*
[translate(@time, ':','')
<
translate('15:59',':','')
][last()]
"/>
</xsl:template>
</xsl:stylesheet>
When the above transformation is applied on the originally provided XML document:
<mainNode>
<sub time="08:00">
<status id="2">On</status>
<status id="3">Off</status>
</sub>
<sub time="13:00">
<status id="4">On</status>
<status id="7">On</status>
</sub>
<sub time="16:00">
<status id="5">On</status>
<status id="6">On</status>
<status id="7">Off</status>
<status id="8">On</status>
</sub>
<sub time="20:00">
<status id="4">Off</status>
<status id="7">On</status>
</sub>
<sub time="23:59">
<status id="4">On</status>
<status id="7">On</status>
</sub>
</mainNode>
the wanted result is produced:
First time after 15:59:
<sub time="16:00">
<status id="5">On</status>
<status id="6">On</status>
<status id="7">Off</status>
<status id="8">On</status>
</sub>
First time before 15:59:
<sub time="13:00">
<status id="4">On</status>
<status id="7">On</status>
</sub>
Do note the following:
The use of the XPath translate()
function to get rid of the colons
The use of the last()
function in the second expression
There is no need to convert the time to seconds before the comparison
When used as part of an XML document (such as an XSLT stylesheet, the <
operator must be escaped.
In XPath 2.0 we can use the following two expressions to produce select the desired nodes:
/*/*[xs:time(concat(@time,':00'))
gt
xs:time('15:59:00')
][1]
selects the first sub
node with time later than 15:59
.
/*/*[xs:time(concat(@time,':00'))
lt
xs:time('15:59:00')
][last()]
selects selects the first sub
node with the previous than 15:59
sub
time.
We can include these in an XSLT 2.0 transformation and check that the really wanted result is produced:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
First time after 15:59:
<xsl:copy-of select=
"/*/*[xs:time(concat(@time,':00'))
gt
xs:time('15:59:00')
][1]
"/>
First time before 15:59:
<xsl:copy-of select=
"/*/*[xs:time(concat(@time,':00'))
lt
xs:time('15:59:00')
][last()]
"/>
</xsl:template>
</xsl:stylesheet>
When the above transformation is applied on the originally provided XML document (the same as in the first solution), the same wanted result is produced.
Do note the following:
xs:time
is a native data type. However, in order to construct an xs:time()
from the values in the xml document, we have to concat to them the missing seconds part.xs:time
values can be compared with the "atomic-value comarison operators" such as lt
or gt
.Upvotes: 2
Reputation: 77141
Well as long as the time is HH:MM something like the following should work: (I must excuse my syntax since I'm just dabbling without running, consider this pseudo-xpath):
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
//sub[fn:compare(@time,'12:59') > 0][1]/status
This should select all the elements where time is greater than 12:59 and then select the first of those elements.
You could also pass the value '12:59' as an external parameter into the xpath evaluation.
Upvotes: 1
Reputation: 189457
Here is the ugly Xpath 1.0 solution:-
sub[number((substring-before(@time, ':')) * 60 + number(substring-after(@time, ':'))) > 959][1]
Note 959 = 15 * 60 + 59 which I'm sure you can do in your calling code.
Give that node the previous node can be accessed as:-
preceding-sibling::sub[1]
However a pragmatic, common sense solution would be to load the XML data into a set of data structures and use a language more suited to this task to look the items up.
Upvotes: 2
Reputation: 6796
If you generate the xml yourself, you could change the way you store the time attribute using an integer value (ticks, for example), then you could do an easy numerical comparison using something like
//sub[@time > 1389893892]
Upvotes: 0