By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,310 Members | 2,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,310 IT Pros & Developers. It's quick & easy.

Manipulating or comparing xs:dateTime values in xpath queries with SelectNodes

P: n/a
Is there a simple way of compare dates? I am trying to create a list of
perishable stock in an inventory that is near or past its expiration date.

I've tried telling it to compare a node's value with a DateTime string, but
that doesn't work.
I've assumed that it is easier to manipulate today's date instead of the
date in my ExpireDate element.

Thankyou in advanced for taking the time to read this and possibly helping
me

Echo 8

----------------------------------------------------------------------------
--------------------------------

My Code:

Public Function FromElement(ByVal Element As Xml.XmlElement) As Boolean

...

Const XmlDateTimeFormat As String = "yyyy-MM-dd\THH:mm:ss\Z"

...

Dim DateToCompare As Date = Now.ToUniversalTime.Subtract(New TimeSpan(7,
0, 0, 0))
Dim DateStr As String = Format(DateToCompare, XmlDateTimeFormat)
Dim ExpiredStocks As Xml.XmlNodeList
' This doesn't work
ExpiredStocks = Element.SelectNodes("child::NS:Food[ExpireDate < " &
DateStr & "]", nsm)

...

Return True

End Function

----------------------------------------------------------------------------
--------------------------------

And my xml data:

<Stocks>
<Stock ID="0001">
<ExpireDate>2004-12-31T12:00:00Z</ExpireDate>
<AmountLeft>17</AmountLeft>
</Stock>

<!-- And so forth -->

</Stocks>
Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Echo 8" <er*******@hotmail.com> wrote in message news:eM**************@tk2msftngp13.phx.gbl...
' This doesn't work
ExpiredStocks = Element.SelectNodes("child::NS:Food[ExpireDate < " &
DateStr & "]", nsm)
Could you describe in a little more detail what doesn't work about
that line?

Does it return an empty XmlNodeList, because the example XML
included contains no namespaces or elements with a local name
of Food? Or have those details just been left out of the example?

(I can't be of much help identifying why the XPath expression doesn't
work when it's inconsistent with the XML.)

Does it throw an exception claiming that there is an invalid qualified
name in the XPath expression, because it sees the colons in the
DateStr as separating a prefix from a local name? This would be
due to the XPath expression missing ' (single-quotes) around the
DateStr to make it appear as a literal string to XPath.
I've assumed that it is easier to manipulate today's date instead of the
date in my ExpireDate element.
You're going to have to manipulate the date in your ExpireDate
element, because Relational Expressions in XPath 1.0 can't
compare strings. Relational operators in XPath require their
operands to be converted to numbers.

http://www.w3.org/TR/xpath#NT-RelationalExpr
Is there a simple way of compare dates?


Something like the following may work for you (untried and w/o
warranty, as the details will depend on what your actual XML is),

ExpiredStocks = Element.SelectNodes( _
String.Format( _
"//Stock[number(translate(ExpireDate,'TZ:-', '.')) < number('{0}')]", _
DateTime.Now.ToString( "yyyyMMdd.HHmmss") _
), _
nsm )

The concept is to strip the dashes, colons and trailing Z, and
change the T to a decimal point, then compare it against a
similarly-formatted DateTime parameter.
Derek Harmon
Nov 12 '05 #2

P: n/a
Sorry about that. I tried to simplify everything until just the "essence" of
what I was trying to get accross. I must have put Food instead of Stock when
I was typing it. Anyway, it does come up with a XmlXPathException about that
qualified name thing. I'm not very experienced with .Net, xpath, or xml, made
obvious by the code I put in the first post.

Anyway, thanks for your swift reply. It's solved my problem.
Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.