homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

how to skip empty nodes?

 3:38 pm on Jul 28, 2008 (gmt 0)


This is my first post here. I hope I'm posting this in the right place. I have an XML document and an ASP page that is designed to iterate though the XML and display the results as a list. The data consists of mailing addresses. All the entries have names, first address line and city/state/zip, but only some addresses have second and third address lines. For a given record I would like to skip writing either the second or third address line entirely if there is no value of the node is empty.

The data looks like this:


After some looking around I concocted an if-then series that has sections that look like:

if //R1SecondAddrNode[not(text())] then
(print line without second addr)

But this does not work. I'm just not sure what the syntax would be.

Can anyone lend a hand? I would sure appreciate it.





 4:51 pm on Jul 28, 2008 (gmt 0)

Hi Michael, welcome to WebmasterWorld!

well, you're right in that you need some kind of condition.

If it were me, I wouldn't rely on XPATH to check if the node is empty. Instead, just grab the value of the node, and then check if it is undefined, null, or a string with a length of zero.

I don't remember my ASP/VB/C# syntax for this, but in PHP I might do something like:

$value = $xml->xpath("R1SecondAddrNode")[0];

or even more simply:
$value = $xml->xpath("R1SecondAddrNode")[0];
print( (empty($value)?"":"<br/>").$value );


 7:31 pm on Jul 28, 2008 (gmt 0)

Thanks for your swift reply! Nice and simple, too. I think I was just trying too hard to produce an XML solution.



 9:06 pm on Jul 28, 2008 (gmt 0)

I'm pretty sure there is a way to do it with XPATH. But why bother :)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved