Testing for an XML attribute

后端 未结 4 1526
轮回少年
轮回少年 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.

    0 讨论(0)
  • 2020-12-30 01:14

    /root/foo[string-length(@src)!=0] return all foo elements have non empty value.

    Unfortunately /root/foo[string-length(@src)=0] indicates elements which don't have src attribute and also elements have src attribute but empty.

    0 讨论(0)
  • 2020-12-30 01:29

    I want to know which elements have a src attribute, which are empty and which have values.

    Elements having a @src attribute which is empty (no string-value):

    //*[@src[not(string())]]
    

    Elements having a @src attribute which has value (string-value):

    //*[string(@src)]
    

    From http://www.w3.org/TR/xpath/#section-String-Functions

    A node-set is converted to a string by returning the string-value of the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.

    From http://www.w3.org/TR/xpath/#function-boolean

    A string is true if and only if its length is non-zero.

    0 讨论(0)
  • 2020-12-30 01:30

    Use:

    //*[@src and not(string-length(@src))]
    

    This selects all elements in the XML document that have a src attribute whose string-value has length of zero.

    //*[@src and string-length(@src)]
    

    This selects all elements in the XML document that have a src attribute whose string-value has length that is not zero.

    //*[@src and string-length(normalize-space(@src))]
    

    This selects all elements in the XML document that have a src attribute whose string-value after excluding the starting and ending whitespace has length that is not zero.

    //[not(@src)]
    

    This selects all elements in the XML document that don't have a src attribute.

    0 讨论(0)
提交回复
热议问题