Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing if nodes empty 1

Status
Not open for further replies.

tripwater

Programmer
Oct 13, 2004
12
US
I am building reports from XML files and sometimes there will be a bill to address and sometimes not. How do I (using XSLT or Javascript)determine if the nodes actually hold info? If they do not have anything in them then I need to build my table displaying the info differently.

For example (this is what I tried but does not work, I am new at this)

code:

Code:
XML
**********
<invoice>
 <billto>
	<firstname>Joe</firstname>
	<lastname>Pesche</lastname>
	<contact></contact>
	<address1>PO Box 5555</address1>
	<address2></address2>
	<city>Youngstown</city>
	<state>OH</state>
	<zip>44503</zip>
	<email></email>
	<priphone>330-744-1222</priphone>
	<secphone></secphone>
 </billto>
</invoice>


XSLT
*********
<xsl:if test="string-length('invoice/address1') &gt; 0">
	<b>Bill to</b>
	<table width="50%" cellpadding="3" border="1">
		<tr>
			<td>
				<xsl:value-of select="invoice/billto/priphone"/><p></p>
				<xsl:value-of select="invoice/billto/lastname"/>,  <xsl:value-of select="invoice/billto/firstname"/><br/>
				<xsl:value-of select="invoice/billto/address1"/><br/>
				<xsl:value-of select="invoice/billto/city"/>,  
				<xsl:value-of select="invoice/billto/state"/> 
				<xsl:value-of select="invoice/billto/zip"/>
			</td>
		</tr>
	</table>				
	</xsl:if>

So ultimately if my XML page does not have info in these fields I do not want any labels or info to show up. SO everything inside the if statement (if empty)I do not want to show up. I have other areas where this is needed so i need a solution so the page is not cluttered with empty data sets.

Thanks for any help with this.
 
This is not finding the address node:
Code:
string-length('invoice/address1')
Try:
Code:
string-length('invoice/billto/address1')
If it's the case that the billto element is not there when there's no bill to address, you can simply use:
Code:
<xsl:if test="invoice/billto">
Jon
 
Thanks JontyMC,

I tried what you posted, and then emptied my XML nodes for the billto and it still shows my empty table with a border around it. I am trying to have nothing show up if there is no info, not even the table.

if (something)
Show billto;
else
show nothing;

I can not seem to get this to work no matter where I place the code you posted. I saw where I screwed up and did not have "billto" in my Xpath and I fixed that as well as tried

<xsl:if test="invoice/billto">

and

<xsl:if test="invoice/billto/address1">

Just to see if it would make a difference but nothing worked.

Got any ideas? Again thanks for your time.
 
Code:
<xsl:if test="invoice/billto">
This will only work if your XML doesn't have the billto element, ie:
Code:
<invoice>
</invoice>
I spotted the mistake in your first way:
Code:
string-length('invoice/billto/address1')
Should be:
Code:
string-length(invoice/billto/address1)
Another way would be to say:
Code:
<xsl:if test="invoice/billto/address1 = ''">
 
You need to make sure you understand the difference between an empty element and a missing element.
Code:
<customer>
  <firstname>Joe</customer>
  <lastname>DiMaggio</lastname>
  <address />
</customer>
This XML has an empty element for address, and using an XPath query like /customer/address/text() != '' will help you with it.
Code:
<customer>
  <firstname>Joe</customer>
  <lastname>DiMaggio</lastname>
</customer>
This XML has no element for address. Using the above query may throw an error, or it may return null (depends on your parser). To check for this, use an XPathQuery like /customer/address and check to see if you got a node back.

In order to prevent this confusion, design an XSD which describes your XML document, and you can set properties in the XSD to control both situations.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top