translate() ?

Posted on 2005-05-09
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
    LVL 8

    Expert Comment

    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.

    LVL 6

    Author Comment

    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

    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

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

    LVL 6

    Author Comment

    thanks alot.

    works a treat!


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now