加入两个XPath查询

时间:2014-03-14 09:08:04

标签: java xml xpath xquery

Hye,我是XML新手阅读我想加入这两个查询!

 /ADOXML/MODELS/MODEL/INSTANCE[@class='Activity']

 /ADOXML/MODELS/MODEL/INSTANCE/ATTRIBUTE[@name='Description' and @type='STRING']

我试过了:

  /ADOXML/MODELS/MODEL/INSTANCE[@class='Activity'] | /ADOXML/MODELS/MODEL/INSTANCE/ATTRIBUTE[@name='Description' and @type='STRING']

但问题是它使用我的代码给出了错误的输出:

NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);

ArrayList<String> text = new ArrayList<String>();

int k;
for (k = 0; k < nodeList.getLength(); k++) {

 String txt=nodeList.item(k).getTextContent();
 txt=txt.replace("[","").replace("]","").replace("#","").replace(":", "").replace("•", "").replace("\n", " ")
       .replace("\u0092", " ").replace("'", "").replace("•", " ").replace("-", " ").replace("’", " ").replace("\n", " ");


text.add(txt);


}

那么如何加入上述两个查询

1 个答案:

答案 0 :(得分:0)

使用/ADOXML/MODELS/MODEL/INSTANCE[@class='Activity'],您正在选择符合条件的INSTANCE;使用/ADOXML/MODELS/MODEL/INSTANCE/ATTRIBUTE[@name='Description' and @type='STRING'],您正在选择带有critera的ATTRIBUTE

更改为:

/ADOXML/MODELS/MODEL/INSTANCE[@class='Activity'] | /ADOXML/MODELS/MODEL/INSTANCE/ATTRIBUTE[@name='Description' and @type='STRING']/..

现在它们都会选择符合不同条件的INSTANCE/..返回一个级别,以选择XPATH中的父级而不是终端元素。