This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Check for null value and Check if child tag exists
- To: xsl-list at lists dot mulberrytech dot com
- Subject: Re: [xsl] Check for null value and Check if child tag exists
- From: Dan Diebolt <dandiebolt at yahoo dot com>
- Date: Mon, 16 Apr 2001 08:04:41 -0700 (PDT)
- Cc: Priya dot P at ITClearingCorp dot com
- Reply-To: xsl-list at lists dot mulberrytech dot com
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