简体   繁体   中英

Find XPATH with namespace based on attribute element

I have the following 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" . 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" ?

You can add the [@type="full"] attribute selector onto 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.

So, t:asset[@type='full'] means select all asset element that have attribute type and value full .

Take a look here xpath synatx .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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