There is no such thing as an 'empty field' in Xml. 'Field' is a term from other technology. There are empty elements or elements with content (text, data, mixed).
So you should not be too anoyed if there is not a way to express it in a single access
Is something like not(B) or not (B/node()) enough to fit your definition of 'empty field'?
Regards
Rick
Hi Folks,
This is a continuation of my previous post…
I showed two XML representations of an empty field in a tab-delimited file. One representation omitted the element:
<Row>
<A>foo</A>
<C>bar</C>
</Row>
Of course, I want to do something with the XML. Specifically, I want an XPath expression that will output “do action” if the field is empty. Since the empty field is represented by no element, this XPath expression will work:
if (not(B)) then "do action" else "no action"
Read as: If there does not exist a B element then …
The second way to represent an empty field is with an empty element:
<Row>
<A>foo</A>
<B/>
<C>bar</C>
</Row>
The XPath expression shown above will not work with the second XML representation. This XPath expression will work:
if (B eq ' ') then "do action" else "no action"
But it will not work with the first XML representation.
So this is the key question:
Is there an XPath expression that will correctly
handle an empty field, regardless of how the field
is represented in XML?
What a beautiful question!
The answer to that question is yes, if we confine ourselves to the two XML representations described above (which seems to be the most typical). Any of the following XPath expressions will handle both XML representations.
David Sewell provided this solution:
if (not(B) or normalize-space(B) eq '') then "do action" else "no action"
Ken Holman provided this solution:
if (not(string(B))) then "do action" else "no action"
David Carlisle provided this solution:
if (B/node()) "d" else "n", "o action"
/Roger