homepage Welcome to WebmasterWorld Guest from 54.211.231.221
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
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?
michael_s




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

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

 

httpwebwitch




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

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

michael_s




msg:3709603
 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.

Michael

httpwebwitch




msg:3709718
 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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved