translate() ?

Posted on 2005-05-09
Medium Priority
Last Modified: 2012-06-21
Hi All

Ive got the contents of a html table which im trying to transform into some kind of XML file.


The table always has 4 cols and the [ position mod 2 =1] will select the 1st and 3rd col which contain a label for the following 2nd or 4th columns

My problem is that im trying to build a new element (col1 or 3) to house its information( cols 2 or 4)

so in the case of


id get col1s name containing col 2 info


Unfortunatly the info is in french so need to strip out/translate the accented chars, spaces or reserved characters.

Can anyone suggest a way that this can be achieved or a better way to reconstruct the data?

Hope you can help.

Question by:metalmickey
  • 2
  • 2

Expert Comment

ID: 13958561
Try it with:-

<xsl:variable name="nodename" select="translate(normalize-space(substring-before(b,':')),' ','_')" />

I think the problem will be with the colon and the spaces in the text.  The above should select out of the text the content preceeding the colon.  Eliminate the leading and trailing whitespace reducing all other whitespace to a single space then replace each of these spaces with an underscore.


Author Comment

ID: 13959342
I think your right, some of the nodes contain single quotes ' and full stops.

Since the html table should ALWAYS contain the same labels could i use some kind of xml mapping to build the node element names. This would kind of translate the values to english too?

<xsl:variable name="nodename">
<if test="b='Réf.'>reference</xsl:if>

Réf. = reference
Prix : = price

Would something like that work?

LVL 23

Expert Comment

ID: 13959348
first things first, if you are not sure about the element name, make it simple for you:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
<xsl:output method="xml" version="utf-8" />
<xsl:template match="/">
<xsl:apply-templates select="//tr/td[position() mod 2 = 1][string-length(b) != 0]" />
<xsl:template match="td">
<item name="{translate(b,'_:','')}" value="{following-sibling::td}" />


XML Output
<?xml version="1.0" encoding="utf-8"?>
  <item name="Réf." value="05363" />
  <item name="Type " value="Maison" />
  <item name="Détail type " value="Au calme" />
  <item name="Code postal &#xD;&#xA;                " value="37310" />
  <item name="Ville " value="Entre BLERE et LOCHES " />
  <item name="Superficie &#xD;&#xA;                " value="300 M²" />
  <item name="Superficie &#xD;&#xA;                  terrasse " value="25 M² " />
  <item name="Prix " value="331 769,00 €" />
  <item name="Nb chambres &#xD;&#xA;                " value="4" />
  <item name="Nb Salle de bain &#xD;&#xA;                  " value="0" />
  <item name="Nb de pièces &#xD;&#xA;                " value="10" />
  <item name="Nb salle d'eau &#xD;&#xA;                  " value="1" />
  <item name="Séjour " value="oui" />
  <item name="Superficie séjour &#xD;&#xA;                  " value="" />
  <item name="Salon " value="2 M² " />
  <item name="Cheminée " value="oui" />
  <item name="Cuisine " value="oui " />
  <item name="Equipée " value="" />
  <item name="Parking " value="" />
  <item name="Garage " value="" />
  <item name="Niveau " value="1 " />
  <item name="ADSL " value="" />
  <item name="Vue " value="" />
  <item name="Année &#xD;&#xA;                  construction " value="17ème " />
  <item name="Exposition &#xD;&#xA;                " value="" />
  <item name="Etat general &#xD;&#xA;                " value="bon " />
  <item name="Chauffage &#xD;&#xA;" value="fuel" />
  <item name="Prestations &#xD;&#xA;                " value="de qualité " />
  <item name="Terrain  " value="arboré. Surface :9600 M² " />
  <item name="Piscine " value="" />
  <item name="Charges " value="" />
  <item name="Taxe foncière &#xD;&#xA;                  " value="494 € " />
  <item name="Taxe habitation " value="600" />
  <item name="Lycée / école " value="" />
  <item name="Marché " value="" />
  <item name="Commerces " value="" />
  <item name="Cours de tennis " value="" />
  <item name="Autoroute " value="" />
  <item name="Gare  " value="" />

LVL 23

Accepted Solution

b1xml2 earned 2000 total points
ID: 13959475
<xsl:template match="td">
<item name="{b}" value="{following-sibling::td}" />

from this you'll have an idea of what characters your are looking to strip...
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
<xsl:output method="xml" version="utf-8" indent="yes"/>
<xsl:param name="char-lists">abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890éè</xsl:param>
<xsl:param name="char-extended">éè</xsl:param>
<xsl:param name="char-replace">ee</xsl:param>
<xsl:param name="char-uppercase">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:param>
<xsl:param name="char-lowercase">abcdefghijklmnopqrstuvwxyz</xsl:param>
<xsl:template match="/">
<xsl:apply-templates select="//tr/td[position() mod 2 = 1][string-length(b) != 0]" />
<xsl:template match="td">
<xsl:variable name="char-target" select="translate(b,$char-lists,'')" />
<xsl:variable name="char-scrubbed" select="translate(b,$char-target,'')" />
<xsl:variable name="char-normal" select="translate($char-scrubbed,$char-extended,$char-replace)" />
<xsl:element name="{translate($char-normal,$char-uppercase,$char-lowercase)}">
<xsl:value-of select="following-sibling::td" />


<?xml version="1.0" encoding="utf-8"?>
  <detailtype>Au calme</detailtype>
  <ville>Entre BLERE et LOCHES </ville>
  <superficie>300 M²</superficie>
  <superficieterrasse>25 M² </superficieterrasse>
  <prix>331 769,00 €</prix>
  <salon>2 M² </salon>
  <cuisine>oui </cuisine>
  <niveau>1 </niveau>
  <anneeconstruction>17ème </anneeconstruction>
  <etatgeneral>bon </etatgeneral>
  <prestations>de qualité </prestations>
  <terrain>arboré. Surface :9600 M² </terrain>
  <taxefonciere>494 € </taxefonciere>


Author Comment

ID: 13960388
thanks alot.

works a treat!


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question