简体   繁体   English

XPath查询:使用条件获取特定值

[英]XPath query : Get specific value with condition

I make a JAVA program to parse xml file like this exemple : 我制作了一个Java程序来解析xml文件,例如:

<lom:contribute>
        <lom:role>
            <lom:source>LOMv1.0</lom:source>
            <lom:value>author</lom:value>
        </lom:role>
        <lom:entity>toto</lom:entity>
        <lom:date>
            <lom:dateTime>2009-10-07</lom:dateTime>
        </lom:date>
    </lom:contribute>
    <lom:contribute>
        <lom:role>
            <lom:source>LOMv1.0</lom:source>
            <lom:value>instructional designer</lom:value>
        </lom:role>
        <lom:entity>titi</lom:entity>
        <lom:date>
            <lom:dateTime>2009-10-07</lom:dateTime>
        </lom:date>
    </lom:contribute>

I would like to make XPath query to get the entity (toto in this exemple) when the role value is author. 当角色值为author时,我想进行XPath查询以获取实体(在此示例中为toto)。 I have make this query for the moment : //*[local-name()='contribute']/*[local-name()='role']/*[local-name()='value'] = 'author' . 我暂时进行了以下查询: //*[local-name()='contribute']/*[local-name()='role']/*[local-name()='value'] = 'author' This query return true if author was present in file but I don't know how to get entity value. 如果文件中存在作者,则此查询返回true,但我不知道如何获取实体值。 Thanks in advance. 提前致谢。

This XPath returns toto : 该XPath返回toto

//*[local-name()='contribute'][*[local-name()='role']/*[local-name()='value'] = 'author']/*[local-name()='entity']/text()

If you remove the last /text() , it returns the entity element containing the text toto . 如果删除最后一个/text() ,它将返回包含文本totoentity元素。 I'm not sure which of them are you looking for. 我不确定您要寻找哪个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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