Results 1 to 7 of 7

Thread: XPath - Resolved.

  1. #1

    Thread Starter
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    XPath - Resolved.

    Right, I'm building a calendar selection. The month & year are known.

    Now, assuming I have an XML structure like this (from Sql Server):
    Code:
    <root>
    	<item>
    		<startdate>2003-08-01T16:00:00</startdate>
    		<enddate>2003-08-03T16:30:00</enddate>
    		<caption>Blah</caption>
    		<url>www.blah.com</url>
    		<color>navy</color>
    	</item>
    	<item>
    		<startdate>2003-08-01T10:00:00</startdate>
    		<enddate>2003-08-07T11:30:00</enddate>
    		<caption>Blah Again</caption>
    		<url>www.blah.com</url>
    		<color>silver</color>
    	</item>
    </root>
    So, with a loop through the days of the month, I've got the date (time can be ignored). Now what I've got to do, is check if the date falls within a date range.
    For example, in the above XML, lets say the date is 20030802. It falls between the start and end dates of both items. Now, I need the colors for both of these items. If the date was 20030804, only the second item's color is required.

    The problem I'm having is that I can't figure out how to use two child nodes in one predicate, using the substring function. This is all being done in C#, btw, and looping through each node in the Xml document & manually checking is easy enough, but why write all that code if you can get it with one XPath query?

    Thanks,
    Brian


    [Edit] Dates were in the wrong format.
    Last edited by axion_sa; Aug 7th, 2003 at 01:07 AM.

  2. #2

    Thread Starter
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    No worries - got it:

    Code:
    root/item[substring(translate(startdate, '-', ''), 1, 8) = '2003-0801'][substring(translate(enddate, '-', ''), 1, 8) <= '2003-0801']

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: XPath - Resolved.

    I don't like digging up old threads but I've got a problem similar, but simpler, that the one you had and I'm wondering if you can figure out what I'm doing wrong. If you take a look at this thread I took the solution you gave in this thread and tried

    ("//privatemessage/"[substring(translate(datestamp, '-', ''),1,10) > '20070101']")

    but got nothing. Any ideas?

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: XPath - Resolved.

    Did you copy that xpath string wrong or is that actually a syntax error?

    (The " after //privatemessage/)

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: XPath - Resolved.

    Copied it wrong. In my program it looks like

    VB Code:
    1. Set oxmlNodeList = XMLdoc.documentElement.selectNodes("//privatemessage/" _
    2.                      & "[substring(translate(datestamp, '-', ''),1,10) > '20070101']")

    which equates to

    ("//privatemessage/[substring(translate(datestamp, '-', ''),1,10) > '20070101']")

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: XPath - Resolved.

    Try
    VB Code:
    1. Set oxmlNodeList = XMLdoc.documentElement.selectNodes("//privatemessage" _
    2.                      & "[substring(translate(datestamp, '-', ''),1,10) > '20070101']")

    Note that the [square brackets] are very similar to a 'WHERE' clause in SQL. So the / that I removed would not have much meaning unless a node were specified after it.

    You might also want to try gt instead of > if the modification didn't work.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: XPath - Resolved.

    Quote Originally Posted by mendhak
    Try
    VB Code:
    1. Set oxmlNodeList = XMLdoc.documentElement.selectNodes("//privatemessage" _
    2.                      & "[substring(translate(datestamp, '-', ''),1,10) > '20070101']")

    Note that the [square brackets] are very similar to a 'WHERE' clause in SQL. So the / that I removed would not have much meaning unless a node were specified after it.

    You might also want to try gt instead of > if the modification didn't work.
    Your code as posted worked. Thanks very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width