我可以在libxml2中使用相对XPath表达式吗?

时间:2010-05-13 16:08:25

标签: c xpath libxml2

我想知道是否可以在libxml2中使用相对的XPath表达式。

这是来自javax.xml.xpath API,我想使用libxml2做类似的事情:

Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
  

参考了   element,一个相对的XPath表达式   现在可以写选择了    子元素:

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "manufacturer";
Node manufacturerNode = (Node) xpath.evaluate(expression, **widgetNode**, XPathConstants.NODE);

2 个答案:

答案 0 :(得分:6)

以下是一些代码示例:

xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);

const xmlChar* xpathExpr = BAD_CAST "//column";
xmlXPathObjectPtr columnXPathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);


// Do whatever you want with your query here. Usually iterate over the result.
// Inside this iteration I do:

cur = columnNodes->nodeTab[i];

// Important part
xpathCtx->node = cur;

// After which you can do something like:
xmlXPathObjectPtr screenXPathObj = xmlXPathEvalExpression(BAD_CAST "screen", xpathCtx); 
xmlNodeSetPtr screenNodes = screenXPathObj->nodesetval;
for (int j = 0; j < screenNodes->nodeNr; j++) {
// You're now iterating over the <screen>s inside the curent <column>
}

希望这有助于某人。

答案 1 :(得分:5)

设置xmlXPathContext对象的节点成员。