Re: [xsl] Check for null value and Check if child tag exists

Subject: Re: [xsl] 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)

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>

   <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:


and have a reference to element city2 that I ignored.


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

 <Country Name="Afghanistan">
  <Region Name="Herat">
   <City Name="Herat" Time="GMT+04:30"></City>
  <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 Name="Albania">
  <City Name="Shkoder" Time="GMT+01:00"></City>
  <City Name="Tirane" Time="GMT+01:00"></City>
 <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>
 <Country Name="American Samoa">
  <City Name="Pago Pago" Time="GMT-11:00"></City>

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

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

Do You Yahoo!?
Get email at your own domain with Yahoo! Mail.

 XSL-List info and archive:

Current Thread