Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Sum function in an XPath expression using ASP [Thread Next] Re: Sum function in an XPath expression using ASPTo: NULL Date: 8/8/2004 1:16:00 PM Atif Sarfraz wrote: > I am trying to do a sum of amounts node using the following XPath > expression sum(//Charges/Charge/Amount). The XPath expression works fine > when I run it on my XML, using an XSL. However I want to use the same > expression through a DOMDocument as follows. > > <% > dim TotalCharge, MyXML > > ' Suppose that I have my xml loaded in a DOMDocument called MyXML. So I > am trying to do a sum of all Charges. > TotalCharge = MyXML.selectSingleNode("sum(//Charges/Charge/Amount)") > > %> > > The above call fails because it expects a node from the XPath > expression, however the value returned is a Number. > Is there some way of making this work? Not with XPath (well not with the XPath API MSXML offers), you can only use that to select the nodes you want to sum up and then you can use script to get the sum: Option Explicit Dim XmlDocument, TotalCharges, Loaded, Amounts, Amount SetLocale("en-us") Set XmlDocument = WScript.CreateObject("Msxml2.DOMDocument.4.0") XmlDocument.async = False Loaded = XmlDocument.Load("test2004080801.xml") If Loaded Then XmlDocument.setProperty "SelectionLanguage", "XPath" Set Amounts = XmlDocument.SelectNodes("//Charges/Charge/Amount/text()") TotalCharges = 0 For Each Amount in Amounts TotalCharges = TotalCharges + CDbl(Amount.nodeValue) Next WScript.Echo "Total is " & TotalCharges & "." End If -- Martin Honnen http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
