[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Check for null value and Check if child tag exists

Subject: Re: Check for null value and Check if child tag exists
From: Dan Diebolt <dandiebolt@xxxxxxxxx>
Date: Mon, 16 Apr 2001 08:04:41 -0700 (PDT)
xsl null value
Priya,

If you have control over the format of the XML, you might consider
distributing the Region Name into an attribute of the City element:

   <Region Name="Herat">
    <City Name="Herat" Time="GMT+04:30"></City>
   </Region>

   <City Region="Herat" Name="Herat" Time="GMT+04:30"></City>

In your original XML, the Region element acts as a "phony" container
merely setting up some City elements or "inherit" a region name.
You might ask why this process couldn't continue with the country
as well. For example:

   <City Country="Afghanistan" Region="Herat" 
      Name="Herat" Time="GMT+04:30"></City>

My advise is to try and use an XML representation that has a 
"balanced tree" if possible - unless there is a good reason
not to. If your data was inherently unbalanced (some attributes
or elements occur only rarely), this is certainly a good reason 
not to. 

On the other hand if you have no control over the XML format, you
have to work with what you are given. In any event, I enclude a verion
that works with your original data noting that you have excluded
at least one region in your desired output:

	<row>
		<country>Afghanistan</country>
		<region/>
		<city>Herat</city>
	</row>


and have a reference to element city2 that I ignored.

Regards,

Dan
************************************************************
File: CountryProblem.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="CountryProblem.xsl"?>

<World>
 <Country Name="Afghanistan">
  <Region Name="Herat">
   <City Name="Herat" Time="GMT+04:30"></City>
  </Region>
  <City Name="Kabul" Time="GMT+04:30"></City>
  <City Name="Kandahar" Time="GMT+04:30"></City>
  <City Name="Mazar-e Sharif" Time="GMT+04:30"></City>
 </Country>
 <Country Name="Albania">
  <City Name="Shkoder" Time="GMT+01:00"></City>
  <City Name="Tirane" Time="GMT+01:00"></City>
 </Country>
 <Country Name="Algeria">
  <Region Name="Algiers">
   <City Name="Alger" Time="GMT+01:00"></City>
   <City Name="Annaba" Time="GMT+01:00"></City>
   <City Name="Blida" Time="GMT+01:00"></City>
   <City Name="Constantine" Time="GMT+01:00"></City>
   <City Name="Oran" Time="GMT+01:00"></City>
   <City Name="Skikda" Time="GMT+01:00"></City>
  </Region>
 </Country>
 <Country Name="American Samoa">
  <City Name="Pago Pago" Time="GMT-11:00"></City>
 </Country>
</World>

File: CountryProblem.xsl
<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   
 <xsl:output indent="yes"/>
	
 <xsl:template match="/">
  <xsl:element name="rowset">
  <xsl:apply-templates select="World"/>
  </xsl:element>
 </xsl:template>
	
 <xsl:template match="World">
  <xsl:apply-templates select="Country"/>
 </xsl:template>
	
 <xsl:template match="Country">
  <xsl:for-each select="Region/City">
   <xsl:element name="row">
    <xsl:element name="country"><xsl:value-of
select="../../@Name"/></xsl:element>
    <xsl:element name="region"><xsl:value-of select="../@Name"/></xsl:element>
    <xsl:element name="city"><xsl:value-of select="@Name"/></xsl:element>
   </xsl:element>
   </xsl:for-each>
    <xsl:for-each select="City">
     <xsl:element name="row">
     <xsl:element name="country"><xsl:value-of
select="../@Name"/></xsl:element>
     <xsl:element name="region"><xsl:value-of select="''"/></xsl:element>
     <xsl:element name="city"><xsl:value-of select="@Name"/></xsl:element>
    </xsl:element>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>


__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.