473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Xpath queries

Hi,
I need to do some Xpath queries.
How to add (or multiply) a values returned by a const?

why this //book/price/text()*10 doesn't work?
what's wrong?

thanks

ps I need to use a great c++ library(*nix) with nice documentation. Any
suggestion?
Any suggestion about a good xpath tutorial with example?
Jan 19 '06 #1
8 1632
In article <pa****************************@rimuovimi.it>,
cerelaz <Ce*****@rimuovimi.it> wrote:
why this //book/price/text()*10 doesn't work?


What do you mean, it doesn't work? What happens when you use it?
What software are you using?

-- Richard
Jan 19 '06 #2
On Thu, 19 Jan 2006 17:40:03 +0000, Richard Tobin wrote:
In article <pa****************************@rimuovimi.it>,
cerelaz <Ce*****@rimuovimi.it> wrote:
why this //book/price/text()*10 doesn't work?
What do you mean, it doesn't work? What happens when you use it?

Nothing.
//book/price/text() return correct nodes but with *10 return nothing
What software are you using?
I use libxml2 in my software. Maybe i don't use correctly the library.
Have you some example of usage?

thank you
-- Richard


Jan 19 '06 #3
cerelaz wrote:
On Thu, 19 Jan 2006 17:40:03 +0000, Richard Tobin wrote:

In article <pa****************************@rimuovimi.it>,
cerelaz <Ce*****@rimuovimi.it> wrote:

why this //book/price/text()*10 doesn't work?


What do you mean, it doesn't work? What happens when you use it?


Nothing.
//book/price/text() return correct nodes but with *10 return nothing


If you could post a short but complete example of your XML, it would
help us help you. It's hard to troubleshoot otherwise.

Ed
Jan 20 '06 #4
>> Nothing.
//book/price/text() return correct nodes but with *10 return nothing


If you could post a short but complete example of your XML, it would
help us help you. It's hard to troubleshoot otherwise.

Ed


Ok, you are right.

I use this code with libxml on linux.
If I set query = "//book/price/text()*10" it doesn't find results and
size is == 0, but with query = "//book/price/text()" it returns
correctly all price.
what's wrong? Thank you very very much.

char* filename = "test.xml";

/* libxml */
xmlDocPtr doc;
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
doc = xmlParseFile(filename);

/* Create xpath evaluation context */
xpathCtx = xmlXPathNewContext(doc);

/* Evaluate xpath expression */
xpathObj = xmlXPathEvalExpression((xmlChar*)query, xpathCtx);

/* Print results */
xmlNodeSetPtr nodes = xpathObj->nodesetval;
xmlNodePtr cur;
int size = (nodes) ? nodes->nodeNr : 0;
for(int i = 0; i < size; ++i) {

if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns;

ns = (xmlNsPtr)nodes->nodeTab[i];
cur = (xmlNodePtr)ns->next;
if(cur->ns) {
printf( "1= namespace \"%s\"=\"%s\" for node %s:%s\n",
ns->prefix, ns->href, cur->ns->href, cur->name);
} else {
printf( "2= namespace \"%s\"=\"%s\" for node %s\n",
ns->prefix, ns->href, cur->name);
}
} else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
cur = nodes->nodeTab[i];
if(cur->ns) {
printf( "3= element node \"%s:%s\"\n", cur->ns->href, cur->name);
xmlChar *key = xmlNodeListGetString(doc, cur, 1); [cut]
} else {
printf( "4= element node \"%s\"\n", cur->name); xmlChar *key =
xmlNodeListGetString(doc, cur, 1);

}
} else {
cur = nodes->nodeTab[i];
printf( "5= node \"name %s\": type %d\n", cur->name, cur->type);
xmlChar *key = xmlNodeGetContent(cur);

xmlFree(key);
}
}
Jan 20 '06 #5
and XML

<bookstore>

<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

repeated. This is a test
Jan 21 '06 #6


cerelaz wrote:

I use this code with libxml on linux.
If I set query = "//book/price/text()*10" it doesn't find results and
size is == 0, but with query = "//book/price/text()" it returns
correctly all price.
The expression
//book/price/text()*10
yields a result of type number and not a node set with any nodes.
So you need to look how that API you use allows you to access the
evaluation result of type number.
I am not good at C but the API doc here
<http://www.xmlsoft.org/html/libxml-xpath.html#xmlXPathObject>
suggests that the xmlXPathObject has a member of name
floatval
and type
double
so accessing
xpathObj = xmlXPathEvalExpression((xmlChar*)query, xpathCtx);


xpathObj->floatval

should give you the number result I suspect.

But I have some doubts that that number is what you are looking for, it
will simply be the first price multiplied by 10.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 21 '06 #7

----
xpathObj->floatval

should give you the number result I suspect.

But I have some doubts that that number is what you are looking for, it
will simply be the first price multiplied by 10.


Exactly I want all price multiplied by 10. I Cannot understand how obtain this values.
Is is impossible for the Xpath semantic?
The API don't say a word about this.

Thanks
Jan 23 '06 #8

cerelaz wrote:
Exactly I want all price multiplied by 10. I Cannot understand how obtain this values.
Is is impossible for the Xpath semantic?


With XPath 1.0 alone you can't do that. XSLT could do that as XSLT is
able to create new nodes (e.g. in your example create new price element
where each price element has the new computed value) while XPath 1.0
only allows you to select existing nodes. So consider writing an XSLT
stylesheet with e.g.
<xsl:template match="price">
<xsl:copy>
<xsl:value-of select="10 * ." />
</xsl:copy>
</xsl:template>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 23 '06 #9

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

Similar topics

4
by: gfrommer | last post by:
Hello everyone, I've been reading through a bunch of XPath tutorials and am confused by a couple items. First, is it possible to have multiple predicates in my XPath statement. For example, the...
6
by: Scott Simpson | last post by:
What is a good tool for running XPath queries on Linux? I have the O'Reilly XPath book and I'm using XPath Visualizer on Windows and that seems to work fine, but I'm looking for something on Linux....
2
by: ree32 | last post by:
When I import an xml document in Visual studio and Genereate as schema from it, and create a dataset from it, it adds this line into to the root element of my xml file -...
1
by: Robert Sander | last post by:
Hi, I have to do some case-insensitive queries on a XML document using XPath. Because XML 1.0 and XPath 1.0 are case-sensitive this is a little bit tricky. Sample document: <books>...
9
by: David Thielen | last post by:
Hi; I need to perform xpath queries on xml under .net 2.0 (ok if it won't run under 1.1). What is the approach that will give me the fastest code? These xpath queries will walk all over the...
10
by: Michael C# | last post by:
OK, here's the deal. I have a small XML file that represents a small database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the...
1
by: hazz | last post by:
This is what Nick Malik suggested from the c# newsgroup. Now that the hectic workweek is over, I can begin to appreciate what he said and break it down into constituent elements to solve. If anyone...
3
by: werD | last post by:
Hello I have an xml document that im currently using a forward only .net repeater on and using some xpath queries to display the data The xml is quite simple <?xml version="1.0"...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
3
by: musicgold | last post by:
Hi, I am new in XML. But I have done coding in VBA, C, and HTML. I am using VBA to extract data from an xml file. I use Xpathbuilder to generate Xpath queries for my work. However, some Xpath...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.