Welcome to WebmasterWorld Guest from 54.145.208.64

Forum Moderators: httpwebwitch

Message Too Old, No Replies

how to skip empty nodes?

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

5+ Year Member



Hello,

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:

<R1>
<Name>blah</Name>
<FirstAddr>Blah</FirstAddr>
<SecondAddr/>
<ThirdAddr/>
<CityState>Blah</CityState>
</R1>

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.

Thanks.

Michael

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

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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];
if(!empty($value)){
print("<br/>".$value);
}

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

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

5+ Year Member



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

Michael

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

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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