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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing comma from end of multiple item text list 1

Status
Not open for further replies.

AJ64

Programmer
May 5, 2006
2
US
i am calling countries from within an xsl file and depending on the number of countries to which a product is available to, the number of countries that gets displayed varies.

what i am trying to do is have a comma and space in between each country that shows up on the html output pages, e.g. Japan, United Kingdom, Spain

is there a way i can code the logic so that the last country doesn't have a comma at the end?

the code i have setup below is limited and the comma shows up for each country, even the last. i imagine there is a way to do this but i'm not sure how.

any help greatly appreciated.

~AJ

<xsl:if test="contains(xmen:TargetedCountries,'AU')">Australia, </xsl:if>
<xsl:if test="contains(xmen:TargetedCountries,'CA')">Canada, </xsl:if>
<xsl:if test="contains(xmen:TargetedCountries,'CN')">China, </xsl:if>
<xsl:if test="contains(xmen:TargetedCountries,'FR')">France, </xsl:if>
<xsl:if test="contains(xmen:TargetedCountries,'DE')">Germany, </xsl:if>
<xsl:if test="contains(xmen:TargetedCountries,'IN')">India, </xsl:if>
<xsl:if test="contains(xmen:TargetedCountries,'IT')">Italy, </xsl:if>
<xsl:if test="contains(xmen:TargetedCountries,'JP')">Japan, </xsl:if>
<xsl:if test="contains(xmen:TargetedCountries,'KR')">Korea, </xsl:if>
<xsl:if test="contains(xmen:TargetedCountries,'GB')">United Kingdom, </xsl:if>
<xsl:if test="contains(xmen:TargetedCountries,'US')">United States, </xsl:if>
<xsl:if test="contains(xmen:TargetedCountries,'ES')">Spain, </xsl:if>
<xsl:if test="contains(xmen:TargetedCountries,'TW')">Taiwan, </xsl:if>
 
Quick solution:
Code:
<xsl:variable name="countryList">
  <xsl:if test="contains(xmen:TargetedCountries,'AU')">Australia, </xsl:if>
  <xsl:if test="contains(xmen:TargetedCountries,'CA')">Canada, </xsl:if>
  <xsl:if test="contains(xmen:TargetedCountries,'CN')">China, </xsl:if>
  <xsl:if test="contains(xmen:TargetedCountries,'FR')">France, </xsl:if>
  <xsl:if test="contains(xmen:TargetedCountries,'DE')">Germany, </xsl:if>
  <xsl:if test="contains(xmen:TargetedCountries,'IN')">India, </xsl:if>
  <xsl:if test="contains(xmen:TargetedCountries,'IT')">Italy, </xsl:if>
  <xsl:if test="contains(xmen:TargetedCountries,'JP')">Japan, </xsl:if>
  <xsl:if test="contains(xmen:TargetedCountries,'KR')">Korea, </xsl:if>
  <xsl:if test="contains(xmen:TargetedCountries,'GB')">United Kingdom, </xsl:if>
  <xsl:if test="contains(xmen:TargetedCountries,'US')">United States, </xsl:if>
  <xsl:if test="contains(xmen:TargetedCountries,'ES')">Spain, </xsl:if>
  <xsl:if test="contains(xmen:TargetedCountries,'TW')">Taiwan, </xsl:if>
</xsl:variable>
<xsl:value-of select="substring($countryList, 0, string-length($countryList - 2))"/>
I'd probably keep the countries in seperate XML file:
Code:
<countries>
  <country id="AU">Australia</country>
  <country id="CA">Canada</country>
  .....
</countries>
Then do:
Code:
<xsl:variable name="TargetedCountries" select="xmen:TargetedCountries"/>
<xsl:variable name="countryList">
  <xsl:for-each select="document('countries.xml')/countries/country">
    <xsl:if test="contains($TargetedCountries, @id)">
      <xsl:value-of select="."/>
      <xsl:text>, </xsl:text>
    </xsl:if>
  </xsl:for-each>
</xsl:variable>
<xsl:value-of select="substring($countryList, 0, string-length($countryList - 2))"/>

Jon

"I don't regret this, but I both rue and lament it.
 
Jon,

thanks so much for the helpful reply and solution. i'm just getting started with xsl and this is great stuff. i like too that you gave a "quick solution" and a more appropriate one.

kudos to you!

~AJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top