XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
Re: [xml-dev] Handling an empty field, regardless of its XML representation

On 22 September 2016 at 19:09, Costello, Roger L. <costello@mitre.org> wrote:
> 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
>
>


note that only mine actually answers the question as asked (that
doesn't mean that the others don't answer the question as intended:-)

Firstly the first one will take <B> </B> with a space as empty whereas
the second two will or will not take it as empty depending on whether
the parser has been instructed to strip white space text nodes from B.

Secondly if B can take element nodes or comments or processing
instructions then the first two will take

<B><apos/><!--this encodes a '--></B>

as empty but the third one will not.

Perhaps in the special case that the XML has been constructed from the
tab separated file by a known process none of these things can happen
and all the expressions are equivalent.

David


[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS