Jump to content

check for empty xml tags


zaheer

Recommended Posts

if for example i have the tags:<name>zaheer</name><name></name>and i want the condition if there is info within the tags do this:document.write(x.getElementsByTagName("name")[0].childNodes[0].nodeValue);else if there's nothing print "nothing", or move onto the next test.how do u do this in javascript. thanx in advance.

Link to comment
Share on other sites

I think with the default a.k.a. "or" operator - "||" like so:

document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue || 'nothing');

or maybe with the ternary operator (if ? then : else) like:

document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue == null ? x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue : 'nothing');

Link to comment
Share on other sites

I think with the default a.k.a. "or" operator - "||" like so:
document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue || 'nothing');

or maybe with the ternary operator (if ? then : else) like:

document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue == null ? x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue : 'nothing');

I have the same problem, solution above does not seem to work for me.
Link to comment
Share on other sites

this worked for me (remember that there are many ways to do this, this is just one of them). I used XML Data Islands for convenience only. I would never do this in a production system

<html>	<head>		<xml id="oXML">			<?xml version="1.0" encoding="ISO-8859-1"?>			<names>				<name>zaheer</name>				<name></name>			</names>		</xml>		<script type="text/javascript">			var xmlDoc;	    	function loadXML()    		{			   var xmlDoc   =     new ActiveXObject("Msxml2.DOMDocument.4.0")			   xmlDoc.async=false;			   xmlDoc.loadXML(oXML.xml);    			var xmlNames = xmlDoc.selectNodes("//name");				for (var l=0;l<xmlNames.length;l++)				{					Item = xmlNames.item(l);					document.write( (Item.text.length != 0) ? Item.text : "Nothing")					document.write("<br/>")				}			}		</script>	</head>	<body onload="loadXML()"></body></html>

Just an aside, if you were rendering using XSLT, you could use

<xsl:for-each select="//name">  <xsl:choose>	<xsl:when test="string(text())">	  <xsl:value-of select="."/>	</xsl:when>	<xsl:otherwise>		<xsl:text>Nothing</xsl:text>	</xsl:otherwise>  </xsl:choose></xsl:for-each>

Link to comment
Share on other sites

what if the xml file looked like this:<item><name></name><address>anwhere</address><tel>somenumber</tel></item><item><name>bob</name><address>someplase</address><tel>number</tel></item>i want the condition if there is info within the tags do this:document.write(x.getElementsByTagName("name")[0].childNodes[0].nodeValue);else if there's nothing within this tag, move into checking the next element (address), and then eventually moving onto the next item to check for empty tags in each element.

Link to comment
Share on other sites

what if the xml file looked like this:<item><name></name><address>anwhere</address><tel>somenumber</tel></item><item><name>bob</name><address>someplase</address><tel>number</tel></item>i want the condition if there is info within the tags do this:document.write(x.getElementsByTagName("name")[0].childNodes[0].nodeValue);else if there's nothing within this tag, move into checking the next element (address), and then eventually moving onto the next item to check for empty tags in each element.
Maybe something like
var writeThis = x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;if(writeThis != null) {document.write(writeThis);}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...