简体   繁体   English

根据属性元素查找具有名称空间的XPATH

[英]Find XPATH with namespace based on attribute element

I have the following xml: 我有以下xml:

<assets>
    <asset type="full">
        <file_name>WME__HD_2CH_EN_16X9_178_2398_FINAL.mov</file_name>
    </asset>
    ...
</assets>

I have multiple asset blocks, so I need to reference the attribute type = "full" . 我有多个asset块,因此我需要引用属性type = "full" This is what I currently have to reference them all -- 这是我目前必须引用的所有内容-

node.xpath("//t:assets/t:asset/t:file_name/text()", 
            namespaces={'t':'http://apple.com/itunes/importer'})

How would I reference only the asset with type = "full" ? 我如何引用type = "full"的资产?

You can add the [@type="full"] attribute selector onto t:asset : 您可以将[@type="full"]属性选择器添加到t:asset

node.xpath("//t:assets/t:asset[@type='full']/t:file_name/text()", 
        namespaces={'t':'http://apple.com/itunes/importer'})

Try this 尝试这个

node.xpath("//t:assets/t:asset[@type='full']/t:file_name/text()", 
        namespaces={'t':'http://apple.com/itunes/importer'})

@ notation is used to select attributes through xpath. @表示法用于通过xpath选择属性。

So, t:asset[@type='full'] means select all asset element that have attribute type and value full . 因此, t:asset[@type='full']意味着选择所有具有属性type和值full asset元素。

Take a look here xpath synatx . 在这里看看xpath synatx

node.xpath("//t:assets/t:asset[@type='full']/t:file_name/text()", 
        namespaces={'t':'http://apple.com/itunes/importer'})

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

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