Flossy May Flossy May - 6 days ago 6
HTML Question

XSLT - Outputting DIVs around several elements. Trouble with not outputting labels if the element is empty

Here is what I am trying to do:

Output the values in the NAME element in ABC order.

In the ECONOMY element, output these elements:

- Economy_overview

- Unemployment_rate

- Public_debt

- Exports

The data for each country should be output in a series of DIV elements.
The DIVs should have a thin black border around them, which will separate each country Each piece of information about the country requires a label in a bold (B) tag.
If the information is missing the label should not be displayed.
For example, here is the output for two countries, one with some info and another with all the info (long paragraphs have been trimmed in this example, the code should just output everything):

<div style="border: 1px solid black; margin: 10px;"> <p>
<b>Country:</b> Adelie Land </p>
</div>
<div style="border: 1px solid black; margin: 10px;"> <p>
<b>Country:</b> Albania </p>
<p>
<b>Economy overview:</b>Lagging behind its Balkan neighbors, Albania ... inflation is low and stable.
</p> <p>
</p> <p>
<b>Public debt:</b> 51.4% of GDP (2007 est.) </p>
<p>
<b>Exports:</b> $1.076 billion f.o.b. (2007 est.)
</p> </div>


There should also be an H1 tag before the DIV tag, but I am not having trouble with that part.

Here is my code:

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
method="xml"
indent="yes" />

<xsl:template match="/WFB">
<html>
<body>
<h1>
<xsl:value-of select="concat('CIA World ', @YEAR, ' Factbook - Countries')"/>
</h1>
<xsl:apply-templates select="PLACE"/>
</body>
</html>
</xsl:template>

<xsl:template match="/WFB">
<xsl:value-of select="NAME"/>
<div style="border: 1px solid black; margin: 10px;">
<p><xsl:value-of select="concat('<b>Country:</b> ', @NAME)"/><p>
<div>
<xsl:for-each select="PLACE">
<xsl:sort select="NAME"/>
<xsl:value-of select="NAME"/>
</xsl:for-each>
</xsl:template>

<xsl:template match="NAME">
<xsl:apply-templates/>
</xsl:template>

<div style="border: 1px solid black; margin: 10px;">
<xsl:template match="NAME/*[string()]" priority="-1"/>

<xsl:template match="Economy_overview">
<p><b>Economy Overview: </b>
<xsl:value-of select="normalize-space()"/></p>
</xsl:template>

<xsl:template match="Unemployment_rate">
<p><b>Unemployment Rate:</b>
<xsl:value-of select="normalize-space()"/></p>
</xsl:template>

<xsl:template match="Public_debt">
<p><b>Public Debt: </b>
<xsl:value-of select="normalize-space()"/></p>
</xsl:template>

<xsl:template match="Exports">
<p><b>Exports: </b>
<xsl:value-of select="normalize-space()"/></p>
</xsl:template>
</div>
</xsl:stylesheet>


I feel like I am overcomplicating this, which tends to happen. When I transform my code, it does not output anything.

Here is my XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<WFB YEAR="2008">
<PLACE ID="Adelie_Land" PART_OF="French_Southern_and_Antarctic_Lands">
<NAME>Adelie Land</NAME>
<INTRODUCTION>
<Background>The only non-insular district of the TAAF is the Antarctic claim known as "Adelie Land." The US Government does not recognize it as a French dependency.</Background>
</INTRODUCTION>
<GEOGRAPHY>
<Location/>
<latitude>78 00 S</latitude>
<longitude>139 00 E</longitude>
<Map_references/>
<Area>
<total>500,000 sq km (approx)</total>
<land>500,000 sq km (approx)</land>
<water>0 sq km</water>
</Area>
</GEOGRAPHY>
<PEOPLE>
<Population/>
<Age_structure/>
<Median_age/>
<Languages/>
<Literacy/>
<People_note/>
</PEOPLE>
<GOVERNMENT>
<Country_name/>
<Dependency_status/>
<Government_type/>
<Capital/>
<Government_note/>
</GOVERNMENT>
<ECONOMY>
<Economy_overview/>
<Unemployment_rate/>
<Population_below_poverty_line/>
<Household_income_or_consumption_by_percentage_share/>
<Distribution_of_family_income_Gini_index/>
<Inflation_rate_consumer_prices/>
<Investment_gross_fixed/>
<Budget/>
<Public_debt/>
<Agriculture_products/>
<Industries/>
<Industrial_production_growth_rate/>
<Oil_production/>
<Oil_consumption/>
<Oil_exports/>
<Oil_imports/>
<Oil_proved_reserves/>
<Natural_gas_proved_reserves/>
<Current_account_balance/>
<Exports/>
<Exports_commodities/>
<Exports_partners/>
<Imports/>
<Imports_commodities/>
<Imports_partners/>
<Reserves_of_foreign_exchange_and_gold/>
<Debt_external/>
<Economic_aid_donor/>
<Economic_aid_recipient/>
<Currency/>
<Exchange_rates/>
<Fiscal_year/>
</ECONOMY>
</PLACE>
<PLACE ID="Afghanistan">
<NAME>Afghanistan</NAME>
<GEOGRAPHY>
<Location>Southern Asia, north and west of Pakistan, east of Iran</Location>
<latitude>33 00 N</latitude>
<longitude>65 00 E</longitude>
<Map_references>Asia</Map_references>
<Area>
<total>647,500 sq km</total>
<land>647,500 sq km</land>
<water>0 sq km</water>
</Area>
<Area_comparative>slightly smaller than Texas</Area_comparative>
</GEOGRAPHY>
<PEOPLE>
<Population>32,738,376 (July 2008 est.)</Population>
<Age_structure>
<age_0_14_years>44.6% (male 7,474,394/female 7,121,145)</age_0_14_years>
<age_15_64_years>53% (male 8,901,880/female 8,447,983)</age_15_64_years>
<age_65_years_and_over>2.4% (male 383,830/female 409,144) (2008 est.)</age_65_years_and_over>
</Age_structure>
</PEOPLE>
<GOVERNMENT>
<Country_name>
<conventional_long_form>Islamic Republic of Afghanistan</conventional_long_form>
<conventional_short_form>Afghanistan</conventional_short_form>
<local_long_form>Jomhuri-ye Eslami-ye Afghanestan</local_long_form>
<local_short_form>Afghanestan</local_short_form>
<former>Republic of Afghanistan</former>
</Country_name>
</GOVERNMENT>
<ECONOMY>
<Economy_overview>
Afghanistan's economy is recovering from decades of conflict. The economy has improved significantly since the fall of the Taliban regime in 2001 largely because of the infusion of international assistance, the recovery of the agricultural sector, and service sector growth. Real GDP growth exceeded 7% in 2007. Despite the progress of the past few years, Afghanistan is extremely poor, landlocked, and highly dependent on foreign aid, agriculture, and trade with neighboring countries. Much of the population continues to suffer from shortages of housing, clean water, electricity, medical care, and jobs. Criminality, insecurity, and the Afghan Government's inability to extend rule of law to all parts of the country pose challenges to future economic growth. It will probably take the remainder of the decade and continuing donor aid and attention to significantly raise Afghanistan's living standards from its current level, among the lowest in the world. International pledges made by more than 60 countries and international financial institutions at the Berlin Donors Conference for Afghan reconstruction in March 2004 reached $8.9 billion for 2004-09. While the international community remains committed to Afghanistan's development, pledging over $24 billion at three donors' conferences since 2002, Kabul will need to overcome a number of challenges. Expanding poppy cultivation and a growing opium trade generate roughly $4 billion in illicit economic activity and looms as one of Kabul's most serious policy concerns. Other long-term challenges include: budget sustainability, job creation, corruption, government capacity, and rebuilding war torn infrastructure.
</Economy_overview>
<GDP_purchasing_power_parity>$35 billion (2007 est.)</GDP_purchasing_power_parity>
<GDP_official_exchange_rate>$8.842 billion (2007 est.)</GDP_official_exchange_rate>
<GDP_real_growth_rate>11.5% (2007 est.)</GDP_real_growth_rate>
<GDP_per_capita>$1,000 (2007 est.)</GDP_per_capita>
<GDP_composition_by_sector>
<agriculture>38%</agriculture>
<industry>24%</industry>
<services>38%</services>
<note>data exclude opium production (2005 est.)</note>
</GDP_composition_by_sector>
<Labor_force>15 million (2004 est.)</Labor_force>
<Labor_force_by_occupation>
<agriculture>80%</agriculture>
<industry>10%</industry>
<services>10% (2004 est.)</services>
</Labor_force_by_occupation>
<Unemployment_rate>40% (2005 est.)</Unemployment_rate>
<Population_below_poverty_line>53% (2003)</Population_below_poverty_line>
<Household_income_or_consumption_by_percentage_share>
<lowest_10pct/>
<highest_10pct/>
</Household_income_or_consumption_by_percentage_share>
<Distribution_of_family_income_Gini_index/>
<Inflation_rate_consumer_prices>13% (2007 est.)</Inflation_rate_consumer_prices>
<Investment_gross_fixed/>
<Budget>
<revenues>$715 million</revenues>
<expenditures>$2.6 billion</expenditures>
<note>Afghanistan has also received $273 million from the Reconstruction Trust Fund and $63 million from the Law and Order Trust Fund (2007 est.)</note>
</Budget>
<Public_debt/>
<Agriculture_products>opium, wheat, fruits, nuts; wool, mutton, sheepskins, lambskins</Agriculture_products>
<Industries>small-scale production of textiles, soap, furniture, shoes, fertilizer, cement; handwoven carpets; natural gas, coal, copper</Industries>
<Industrial_production_growth_rate/>
<Electricity_production>839 million kWh (2007 est.)</Electricity_production>
<Electricity_consumption>1.088 billion kWh (2006 est.)</Electricity_consumption>
<Electricity_exports>0 kWh (2007 est.)</Electricity_exports>
<Electricity_imports>230 million kWh (2007 est.)</Electricity_imports>
<Oil_production>0 bbl/day (2007 est.)</Oil_production>
<Oil_consumption>5,036 bbl/day (2006 est.)</Oil_consumption>
<Oil_exports>0 bbl/day (2005)</Oil_exports>
<Oil_imports>4,534 bbl/day (2005)</Oil_imports>
<Oil_proved_reserves>0 bbl (1 January 2006 est.)</Oil_proved_reserves>
<Natural_gas_production>20 million cu m (2006 est.)</Natural_gas_production>
<Natural_gas_consumption>20 million cu m (2006 est.)</Natural_gas_consumption>
<Natural_gas_exports>0 cu m (2007 est.)</Natural_gas_exports>
<Natural_gas_imports>0 cu m (2007 est.)</Natural_gas_imports>
<Natural_gas_proved_reserves>49.55 billion cu m (1 January 2008 est.)</Natural_gas_proved_reserves>
<Current_account_balance/>
<Exports>$274 million; note - not including illicit exports or reexports (2006)</Exports>
<Exports_commodities>opium, fruits and nuts, handwoven carpets, wool, cotton, hides and pelts, precious and semi-precious gems</Exports_commodities>
<Exports_partners>India 22.8%, Pakistan 21.8%, US 20.5%, Tajikistan 7.2% (2007)</Exports_partners>
<Imports>$3.823 billion (2006)</Imports>
<Imports_commodities>capital goods, food, textiles, petroleum products</Imports_commodities>
<Imports_partners>Pakistan 36.8%, US 11%, India 5%, Germany 4.2% (2007)</Imports_partners>
<Reserves_of_foreign_exchange_and_gold/>
<Debt_external>$8 billion in bilateral debt, mostly to Russia; Afghanistan has $500 million in debt to Multilateral Development Banks (2004)</Debt_external>
<Economic_aid_donor/>
<Economic_aid_recipient>$2.775 billion (2005)</Economic_aid_recipient>
<Currency>afghani (AFA)</Currency>
<Exchange_rates>afghanis (AFA) per US dollar - NA (2007), 46 (2006), 47.7 (2005), 48 (2004), 49 (2003)</Exchange_rates>
<Fiscal_year>21 March - 20 March</Fiscal_year>
</ECONOMY>
</PLACE>
</WFB>


NOTE: I removed many of the unnecessary elements because the length was getting way too long. But the elements that I did remove shouldn't affect what I am currently trying to do with the XML document. I included the closing
</PLACE>
and
</WFB>
.

Answer

Consider separating into needed templates including WFB and PLACE and conditionally for the ECONOMY nodes:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
              doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" method="xml" indent="yes"/>

  <xsl:template match="/WFB">
    <html>
      <body>
        <h1>
          <xsl:value-of select="concat('CIA World ', @YEAR, ' Factbook - Countries')"/>
        </h1>
        <xsl:apply-templates select="PLACE">
          <xsl:sort select="NAME" order="ascending" data-type="text"/>
        </xsl:apply-templates>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="PLACE">    
    <div style="border: 1px solid black; margin: 10px;">
      <p>
        <b>Country: </b><xsl:value-of select="NAME"/>
      </p>  
      <xsl:apply-templates select="ECONOMY/Economy_overview"/>
      <xsl:apply-templates select="ECONOMY/Unemployment_rate"/>
      <xsl:apply-templates select="ECONOMY/Public_debt"/>
    </div>
  </xsl:template>

  <xsl:template match="Economy_overview[.!='']">
    <p><b>Economy Overview: </b><xsl:value-of select="."/></p>
  </xsl:template>

  <xsl:template match="Unemployment_rate[.!='']">
    <p><b>Unemployement Rate: </b><xsl:value-of select="."/></p>
  </xsl:template>

  <xsl:template match="Public_debt[.!='']">
    <p><b>Public debt: </b><xsl:value-of select="."/></p>
  </xsl:template>

 </xsl:stylesheet>

HTML Output

Comments