Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 116241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:08:48+00:00 2026-05-11T03:08:48+00:00

Hi, this is the content of my XML file: <?xml version=1.0 encoding=ISO-8859-1?> <mainNode> <sub

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:08:49+00:00Added an answer on May 11, 2026 at 3:08 am

    Here are two solutions:

    I. XPath 1.0

    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, ':','')           &lt;             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:

    1. The use of the XPath translate() function to get rid of the colons

    2. The use of the last() function in the second expression

    3. There is no need to convert the time to seconds before the comparison

    4. When used as part of an XML document (such as an XSLT stylesheet, the < operator must be escaped.

    II. XPath 2.0

    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:

    1. In XPath 2.0 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.
    2. In XPath 2.0 xs:time values can be compared with the ‘atomic-value comarison operators‘ such as lt or gt.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I create xml file with this format using c#? <?xml version=1.0 encoding=utf-8
I have sitemesh with spring working, this is the configuration: decorator.xml <?xml version=1.0 encoding=UTF-8?>
I have example.xml <?xml version=1.0 encoding=UTF-8 standalone=no?> <Document xmlns=urn:iso:std:iso:20022:tech:xsd:pain.001.001.02> <content> <books> <book> <qty>12</qty> <title>C++</title>
I have to replace the content of this xml string through java <My:tag>value_1 22&#xA;value_2
I'm working on an application that gets content from feeds in C#. This content
I have this script to generate an XML file for an RSS feed. Works
looks like I need help again! :-/ Im trying to read this XML file
Given this XML ... <ListBucketResult xmlns=http://s3.amazonaws.com/doc/2006-03-01/> <Name>public.rpmware.com</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <IsTruncated>false</IsTruncated> <Contents> <Key>0.dir</Key> <LastModified>2008-06-25T16:09:49.000Z</LastModified>
I'm getting an error for this content template within a style: Must specify both
I have a window in WPF which shows some media contents. This content contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.