XPath 1.0 条件:select a 如果 b 的值为真

XPath 1.0 condition: select a if b has value of true

我有这个简单的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<json encoding="UTF-8" has-bom="false" html-processing-enabled="true" replaceEntities="true" version="1.0">
    <map>
        <element isString="1" text="key">GREETING_ID</element>
        <element isString="1" text="value">hello</element>
        <element isString="1" text="comment">this is a greeting</element>
        <element isString="0" text="maxLength">5</element>
        <element isString="0" text="needsTranslation">true</element>
    </map>
    <map>
        <element isString="1" text="key">FAREWELL_ID</element>
        <element isString="1" text="value">bye</element>
        <element isString="1" text="comment">this is a farewell</element>
        <element isString="0" text="maxLength">10</element>
        <element isString="0" text="needsTranslation">false</element>
    </map>
</json>

我想要 select 具有属性 text="value" 的元素节点,但前提是在同一 Map 节点中具有属性 text="needsTranslation" 的文本等于 "true".我刚刚开始我的 XPath 之旅,我什至不知道从哪里开始。

你可以尝试类似的方法(我已经用你的 XML 和 return hello 测试过它):

//map[element[@text="needsTranslation" and text()="true"]]/element[@text="value"]

这个//map[element[@text="needsTranslation" and text()="true"]]select一个

Map node element with attribute text="needsTranslation" has text equal to "true"

然后 /element[@text="value"] select

Element node with attribute text="value"