Xpath queries

C

cerelaz

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?
 
R

Richard Tobin

cerelaz said:
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
 
C

cerelaz

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
 
E

Ed Beroset

cerelaz said:
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
 
C

cerelaz

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->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns;

ns = (xmlNsPtr)nodes->nodeTab;
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->type == XML_ELEMENT_NODE) {
cur = nodes->nodeTab;
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;
printf( "5= node \"name %s\": type %d\n", cur->name, cur->type);
xmlChar *key = xmlNodeGetContent(cur);

xmlFree(key);
}
}
 
C

cerelaz

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
 
M

Martin Honnen

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.
 
C

cerelaz

----
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
 
M

Martin Honnen

cerelaz said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top