473,804 Members | 3,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculations inside XPath expressions?

Hi,

I just ran into an interesting situation, where I was not able to write
an xpath expression doing what I want it to. Mabe someone here can help
me ... I hope ;)

Here a sample xml-file I want to do the query on:
<?xml version="1.0" encoding="UTF-8"?>
<cart>
<product count="2" price="10.00" id="sdkjfhdaf-A"/>
<product count="1" price="10.00" id="sdkjfhdaf-A"/>
<product count="1" price="30.00" id="sdkjfhdaf-B"/>
<product count="4" price="12.50" id="sdkjfhdaf-C"/>
<product count="2" price="16.00" id="sdkjfhdaf-A"/>
<product count="3" price="119.00" id="sdkjfhdaf-C"/>
<product count="2" price="13.99" id="sdkjfhdaf-A"/>
<product count="10" price="12.34" id="sdkjfhdaf-B"/>
<product count="3" price="11.00" id="sdkjfhdaf-B"/>
<product count="1" price="1.00" id="sdkjfhdaf-A"/>
</cart>

I would like to test, if the total price for all products in the cart
which have an Id containing "-C" in their id-attribute is biger than
lets say 100.
I have no problem selecting either the price or count attribute of these
but I am unable to get "price * count".
Any suggestions?

I created a workaround by adding an additional total-attribute and
everything is working fine, it's just my scientiffic curiosity ;)

Chris
Jul 20 '05 #1
4 3866


Christofer Dutz wrote:

Here a sample xml-file I want to do the query on:
<?xml version="1.0" encoding="UTF-8"?>
<cart>
<product count="2" price="10.00" id="sdkjfhdaf-A"/>
<product count="1" price="10.00" id="sdkjfhdaf-A"/>
<product count="1" price="30.00" id="sdkjfhdaf-B"/>
<product count="4" price="12.50" id="sdkjfhdaf-C"/>
<product count="2" price="16.00" id="sdkjfhdaf-A"/>
<product count="3" price="119.00" id="sdkjfhdaf-C"/>
<product count="2" price="13.99" id="sdkjfhdaf-A"/>
<product count="10" price="12.34" id="sdkjfhdaf-B"/>
<product count="3" price="11.00" id="sdkjfhdaf-B"/>
<product count="1" price="1.00" id="sdkjfhdaf-A"/>
</cart>

I would like to test, if the total price for all products in the cart
which have an Id containing "-C" in their id-attribute is biger than
lets say 100.
I have no problem selecting either the price or count attribute of these
but I am unable to get "price * count".
Any suggestions?


XPath (at least version 1.0) alone can't do that, you need an XSLT 1.0
stylesheet with a template that adds up the values as necessary:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:styleshe et
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:param name="matchSubs tring" select="'-C'" />

<xsl:template match="/">
<results>
<xsl:call-template name="getTotal" >
<xsl:with-param name="products"
select="cart/product[contains(@id, $matchSubstring )]" />
</xsl:call-template>
</results>
</xsl:template>

<xsl:template name="getTotal" >
<xsl:param name="products" />
<xsl:param name="currentTo tal" select="0" />
<xsl:variable name="currentPr oduct" select="$produc ts[1]" />
<xsl:choose>
<xsl:when test="not($curr entProduct)">
<total>
<xsl:value-of select="$curren tTotal" />
</total>
</xsl:when>
<xsl:otherwis e>
<xsl:call-template name="getTotal" >
<xsl:with-param name="products" select="$produc ts[position()
&gt; 1]" />
<xsl:with-param name="currentTo tal" select="$curren tTotal +
$currentProduct/@count * $currentProduct/@price" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2
Hmmm ... I hoped to get a different answer :( ... well that might be the
reason why I was not able to do it ;)
What about newer XPath Versions?
Adding an XSL transformation to this would be some kind of overkill ...
as I said, was just curiosity.

Thanks anyway,
Chris

Martin Honnen wrote:
XPath (at least version 1.0) alone can't do that, you need an XSLT 1.0
stylesheet with a template that adds up the values as necessary:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:styleshe et
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:param name="matchSubs tring" select="'-C'" />

<xsl:template match="/">
<results>
<xsl:call-template name="getTotal" >
<xsl:with-param name="products" select="cart/product[contains(@id,
$matchSubstring )]" />
</xsl:call-template>
</results>
</xsl:template>

<xsl:template name="getTotal" >
<xsl:param name="products" />
<xsl:param name="currentTo tal" select="0" />
<xsl:variable name="currentPr oduct" select="$produc ts[1]" />
<xsl:choose>
<xsl:when test="not($curr entProduct)">
<total>
<xsl:value-of select="$curren tTotal" />
</total>
</xsl:when>
<xsl:otherwis e>
<xsl:call-template name="getTotal" >
<xsl:with-param name="products" select="$produc ts[position()
&gt; 1]" />
<xsl:with-param name="currentTo tal" select="$curren tTotal +
$currentProduct/@count * $currentProduct/@price" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

Jul 20 '05 #3
Christofer Dutz wrote:
Hmmm ... I hoped to get a different answer :( ... well that might be the
reason why I was not able to do it ;)
What about newer XPath Versions?
Are you really sure you need XPath ?
Adding an XSL transformation to this would be some kind of overkill ...
as I said, was just curiosity.


There are other solutions. Even simple ones.
If other tools are acceptable to you,
I promise to supply a solution (in XMLgawk).
Jul 20 '05 #4


Christofer Dutz wrote:

What about newer XPath Versions?


With XPath 2.0 you could do it with a single expression, using the sum
function on the result returned from a for..in expression:
sum(for $product in /cart/product[contains(@id, $matchSubstring )]
return $product/@count * $product/@price)

Saxon 8 implements XPath 2.0 while the spec is being developed:
<http://saxon.sourcefor ge.net/#F8.4SA>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5831
by: Kevin | last post by:
I know this has probably been discussed many times before (I found answers when I searched yesterday), but I still can't get it to work... I have an attribute @OID that can contain any characters (including &quot; and &apos;) I have java code using Xerces to select a node based on it's @OID attribute using an XPath like this:
7
16661
by: Sebastian Petzelberger | last post by:
Hi group, please give me an example of a xpath with regex or better a link with examples. Thanks in advance, Sebastian
4
4577
by: Daniel | last post by:
Is it possible to use regular expressions inside of an xpath statement executed by System.Xml.XmlDocument.SelectSingleNode() ? string sdoc = "<foo><bar a='1'/><bar a='2'/></foo>"; System.Xml.XmlDocument pdoc = new System.Xml.XmlDocument(); pdoc.LoadXml(sdoc); System.Xml.XmlNode pnode = pdoc.SelectSingleNode("//foo/bar/matches(.,'\\d')"); string foo = pnode.InnerText; int i23 = 23 + 23;
1
2234
by: Arndt Jonasson | last post by:
The definition of XPath 1.0 says "A QName in the node test is expanded into an expanded-name using the namespace declarations from the expression context. This is the same way expansion is done for element type names in start and end-tags except that the default namespace declared with xmlns is not used: if the QName does not have a prefix, then the namespace URI is null (this is the same way attribute names are expanded). It is an error...
4
9133
by: syed.akhlaq | last post by:
Hi, Does anyone know how can I validate XPath expressions using xsd schema? Thanks
4
2804
by: Weston | last post by:
Are there any quick, reliable shortcuts to determine if two arbitrary XPath expressions yield result sets that intersect? The obviously way would be to iterate over the set of nodes from one expression, checking each one to see if it's also contained in the other set, but I'm wondering if there might be ways of doing this by looking at or rewriting the actual XPath expressions themselves, without evaluating them.
1
3115
by: reyesvsn | last post by:
Hello everybody, I have a question concerning XPath expressions and namespaces. Consider this XML: <?xml version="1.0" encoding="UTF-8"?> <newsMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rtr="http://www.reuters.com/ns/2003/08/content" xmlns="http://iptc.org/std/nar/2006-10-01/"> <header>
4
9047
by: ducttape | last post by:
Hi, I have been trying for several days to read XML files into my program. I have been following several good tutorials on the internet, but I am struggling because the particular XML files that I am reading have several nested fields: <?xml version="1.0" encoding="UTF-8" ?> <xml> <records> <record> <database name="test.enl">test</database> <contributors> <authors> <author>
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9587
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.