简体   繁体   中英

vtd-xml xpath, how to use variable expression?

I'm trying to get definitions for a specific word which match to user's input string from an xml:

<?xml version="1.0" encoding="UTF-8" ?>
<diction>
<article>
<key>a</key>
<definition>
    ένας, μια, μία, ένα
</definition>
</article>
<article>
<key>a</key>
<definition>
α (στερητικό)
</definition>
</article>
<article>
<key>all</key>
<definition>
όλος, όλη, όλο
</definition>
</article>
<article>
<key>a.m</key>
<definition>
π.μ., πρωί
</definition>
</article>
<article>
<key>base</key>
<definition>
base
</definition>
</article>
</diction>

code:

ap.declareVariableExpr("myexpr", xmlInput.getText().toString().replaceAll("\\s+$", ""));

ap.selectXPath("/diction/article/key[local-name()=string($myexpr)]");
//ap.evalXPathToString();
//ap.selectXPath("/diction/article/key");
ap.bind(vn);

int i;
while ((i=ap.evalXPath()) !=-1){
    i=vn.getText();
    vn.push();
    if(vn.toElement(VTDNav.NEXT_SIBLING, "definition")) {
        int j = vn.getText();
        if (j != -1)

        parsedData=parsedData+"word: " +        vn.toNormalizedString(i) + "\n"
        + "definition: " + vn.toNormalizedString(j) +"\n";
        Log.i("first", parsedData);
    }
    vn.pop();
}
ap.resetXPath();

The above code gives me all words/definitions and not only those who match with the input string. Is the xpath expression correct? What I'm doing wrong?

AutoPilot中有一个称为defineVariableExpression()的函数,该函数使您可以声明变量表达式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM