translate() ?

Hi All

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

http://homepages.nildram.co.uk/~natkins/table/

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

 <tr>
      <td>
        <b>Réf.</b>
      </td>
      <td>05363</td>
      <td/>
      <td/>
 </tr>


id get col1s name containing col 2 info

<ref>05363</ref>

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.

MetalMickey
LVL 6
metalmickeyAsked:
Who is Participating?
 
b1xml2Commented:
<xsl:template match="td">
<item name="{b}" value="{following-sibling::td}" />
</xsl:template>

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"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fo="http://www.w3.org/1999/XSL/Format"
      exclude-result-prefixes="fo">
<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="/">
<root>
<xsl:apply-templates select="//tr/td[position() mod 2 = 1][string-length(b) != 0]" />
</root>
</xsl:template>
 
<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" />
</xsl:element>
</xsl:template>


</xsl:stylesheet>


XML
====
<?xml version="1.0" encoding="utf-8"?>
<root>
  <ref>05363</ref>
  <type>Maison</type>
  <detailtype>Au calme</detailtype>
  <codepostal>37310</codepostal>
  <ville>Entre BLERE et LOCHES </ville>
  <superficie>300 M²</superficie>
  <superficieterrasse>25 M² </superficieterrasse>
  <prix>331 769,00 €</prix>
  <nbchambres>4</nbchambres>
  <nbsalledebain>0</nbsalledebain>
  <nbdepieces>10</nbdepieces>
  <nbsalledeau>1</nbsalledeau>
  <sejour>oui</sejour>
  <superficiesejour></superficiesejour>
  <salon>2 M² </salon>
  <cheminee>oui</cheminee>
  <cuisine>oui </cuisine>
  <equipee></equipee>
  <parking></parking>
  <garage></garage>
  <niveau>1 </niveau>
  <adsl></adsl>
  <vue></vue>
  <anneeconstruction>17ème </anneeconstruction>
  <exposition></exposition>
  <etatgeneral>bon </etatgeneral>
  <chauffage>fuel</chauffage>
  <prestations>de qualité </prestations>
  <terrain>arboré. Surface :9600 M² </terrain>
  <piscine></piscine>
  <charges></charges>
  <taxefonciere>494 € </taxefonciere>
  <taxehabitation>600</taxehabitation>
  <lyceeecole></lyceeecole>
  <marche></marche>
  <commerces></commerces>
  <coursdetennis></coursdetennis>
  <autoroute></autoroute>
  <gare></gare>
</root>

0
 
anthonywjones66Commented:
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.

Anthony.
0
 
metalmickeyAuthor Commented:
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?

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

Réf. = reference
Prix : = price

Would something like that work?

MM
0
 
b1xml2Commented:
first things first, if you are not sure about the element name, make it simple for you:
XSLT
====
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fo="http://www.w3.org/1999/XSL/Format"
      exclude-result-prefixes="fo">
<xsl:output method="xml" version="utf-8" />
<xsl:template match="/">
<root>
<xsl:apply-templates select="//tr/td[position() mod 2 = 1][string-length(b) != 0]" />
</root>
</xsl:template>
 
<xsl:template match="td">
<item name="{translate(b,'_:','')}" value="{following-sibling::td}" />
</xsl:template>


</xsl:stylesheet>


XML Output
========
<?xml version="1.0" encoding="utf-8"?>
<root>
  <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="" />
</root>


0
 
metalmickeyAuthor Commented:
thanks alot.

works a treat!

MM
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.