Testing for an XML attribute

隐身守侯 提交于 2019-12-03 01:24:12

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.

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.

/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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!