xpath where子句

Val*_*rie 7 xml xpath

我试图在以下xml文件上构建一个xpath查询:

<recordGroup>
    <records>
        <year>1985</year>   
        <album>
            <name> album1 </name>
            <artist>artist1 </artist>
            <label> labelA</label>
        </album>    
        <album>
            <name>album2 </name>
            <artist>artist2 </artist>
            <label> labelB</label>
        </album>    
    </records>      
    <records>
        <year>1986</year>   
        <album>
            <name>album3 </name>
            <artist> artist1 </artist>
            <label>labelC</label>
        </album>    
        <album>
            <name>album4 </name>
            <artist>artist2</artist>
            <label> labelA</label>
        </album>    
    </records>

</recordGroup>
Run Code Online (Sandbox Code Playgroud)

我想检索以下查询:选择所有记录(艺术家,姓名和年份),其中label ='LabelA'.

XML结构可能不适合显示此数据但我从其他软件获取此流,因此,我无法更改它.

有什么建议?

Gab*_*oli 9

这将带回records包含labelA标签的专辑

//records[album/label='labelA']
Run Code Online (Sandbox Code Playgroud)

你也可以用

//records[album/label='labelA']/*[self::year or self::album[label='labelA']]
Run Code Online (Sandbox Code Playgroud)

这将只返回year和充分album的匹配labelA