[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Handling an empty field, regardless of its XML representat

  • From: Rick Jelliffe <rjelliffe@allette.com.au>
  • To: xml-dev@lists.xml.org, "Costello, Roger L." <costello@m...>
  • Date: Sun, 25 Sep 2016 11:10:42 +1000

Re:  Handling an empty field

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


On 23/09/2016 4:10 AM, "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

 




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


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.