Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Evaluate xpath sum() function with msxml

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 3/2/2005 2:09:00 PM

Xerox wrote:

> Can I not execute the xpath without a stylesheet?
> 
> I want something like this: totalPrice = XMLDocument.Evaluate
> ("sum(//item/@price)")

MSXML doesn't provide an API for that so you would need to write a 
function of your own e.g. with JScript that builds up a stylesheet to 
get that result:

function evaluateXPathExpression (node, xpathExpression) {
   var xslDocument = new ActiveXObject('Msxml2.DOMDocument');
   xslDocument.async = false;
   var loaded = xslDocument.loadXML([
'<xsl:stylesheet ',
'xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">',
'<xsl:output method="text" encoding="utf-16" />',
'<xsl:template match="/">',
'<xsl:value-of select="' + xpathExpression + '" />',
'</xsl:template>',
'</xsl:stylesheet>'
].join('\r\n'));
   if (loaded) {
     return node.transformNode(xslDocument);
   }
   else {
     return null
   }
}

var xmlDocument = new ActiveXObject('Msxml2.DOMDocument');
xmlDocument.async = false;
xmlDocument.load('test2005030202.xml');

var result = evaluateXPathExpression(xmlDocument, "sum(//item/@price)");

That approach needs refinement to make sure the quotes in the XPath 
expression passed to function do not clash with quotes in the XSL 
stylesheet but I think with MSXML you have no other way to evaluate an 
expression not yielding a node set or single node.


-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent