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

Handling an empty field, regardless of its XML representation

  • From: "Costello, Roger L." <costello@mitre.org>
  • To: "xml-dev@lists.xml.org" <xml-dev@lists.xml.org>
  • Date: Thu, 22 Sep 2016 18:09:40 +0000

Handling an empty field

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.