Forum Moderators: open
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
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 );