Testing for an XML attribute

后端 未结 4 1528
轮回少年
轮回少年 2020-12-30 00:52

I have a piece of XML like so:


    
    
    

I want t

4条回答
  •  庸人自扰
    2020-12-30 01:13

    This will select the foos with no src attribute.

    /root/foo[not(@src)]
    

    For the other two tasks, I would use a mix of the expressions pointed out by @TOUDIdel and @Dimitre Novatchev: /root/foo[@src and string-length(@src)=0] for foos with an empty src, and /root/foo[@src and string-length(@src)!=0] for foos with an src with content in it.

    As an aside, I would avoid using the "anywhere" selector, // (not to mention the * wildcard), unless you're sure that this is specifically what you need. // is like making your very eager dog sniff a piece of cloth and telling it, "bring me everything that smells like this, wherever you find it". You won't believe the weird crap it can decide to bring back.

提交回复
热议问题