FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour TRANFORMAR CARACTER ESPECIALES DE XML
Posts: 537
Joined: Mon Jan 16, 2006 03:42 PM
TRANFORMAR CARACTER ESPECIALES DE XML
Posted: Fri Nov 06, 2015 02:26 PM

Hola amigos necesito esto:
Carácter Especial
& DEBE QUEDAR ASI &
< DEBE QUEDAR ASI &lt;
> DEBE QUEDAR ASI &gt;
“ DEBE QUEDAR ASI &quot;
‘ DEBE QUEDAR ASI &apos;

EJEMPLO:
PINTURA B&W AFECTO

DEBE QUEDAR ASI

PINTURA B&amp;W AFECTO

salduos

Posts: 392
Joined: Tue Jul 29, 2008 01:55 PM
Re: TRANFORMAR CARACTER ESPECIALES DE XML
Posted: Sat Nov 07, 2015 01:43 AM
Hola

Esta rutina la uso , espero te sirva :
Code (fw): Select all Collapse
**---------------------------------------------------------------------------**
FUNCTION TRANS_VALIDACADENA(_CADENA)
**---------------------------------------------------------------------------**
LOCAL X:=LEN(_CADENA)

_CADENA := STRTRAN(_CADENA,"&","&amp;")
_CADENA := STRTRAN(_CADENA,">",">")
_CADENA := STRTRAN(_CADENA,"<","&lt")
_CADENA := STRTRAN(_CADENA,"|","")
_CADENA := STRTRAN(_CADENA,'"',"&quot;")
_CADENA := STRTRAN(_CADENA,"'","&quot;")
_CADENA := STRTRAN(_CADENA,"´","&apos;")
_CADENA := STRTRAN(_CADENA,"/","")
_CADENA := STRTRAN(_CADENA,"<-","")
_CADENA := STRTRAN(_CADENA,"->","")
_CADENA := STRTRAN(_CADENA,"<?","")
_CADENA := STRTRAN(_CADENA,"?>","")
_CADENA := STRTRAN(_CADENA,"=","")
_CADENA := STRTRAN(_CADENA,"Á","A")
_CADENA := STRTRAN(_CADENA,"É","E")
_CADENA := STRTRAN(_CADENA,"Í","I")
_CADENA := STRTRAN(_CADENA,"Ó","O")
_CADENA := STRTRAN(_CADENA,"Ú","U")
_CADENA := STRTRAN(_CADENA,"á","a")
_CADENA := STRTRAN(_CADENA,"é","e")
_CADENA := STRTRAN(_CADENA,"í","i")
_CADENA := STRTRAN(_CADENA,"ó","o")
_CADENA := STRTRAN(_CADENA,"ú","u")
_CADENA := STRTRAN(_CADENA,"?","")
_CADENA := STRTRAN(_CADENA,"°","")
_CADENA := STRTRAN(_CADENA,"¡","")
_CADENA := STRTRAN(_CADENA,"!","")
_CADENA := STRTRAN(_CADENA,"¿","")
_CADENA := STRTRAN(_CADENA,"'","")
_CADENA := STRTRAN(_CADENA,"^","")
_CADENA := STRTRAN(_CADENA,"`","")
_CADENA := STRTRAN(_CADENA,"~","")
_CADENA := STRTRAN(_CADENA,"¬","")
_CADENA := STRTRAN(_CADENA,":","")
_CADENA := STRTRAN(_CADENA,"%","")
_CADENA := STRTRAN(_CADENA,"#","")
// para html
//_CADENA := STRTRAN(_CADENA,"ñ","&ntilde;")
//_CADENA := STRTRAN(_CADENA,"Ñ","&Ntilde;")
_CADENA := STRTRAN(_CADENA,"¿","&iquest;")
_CADENA := STRTRAN(_CADENA,"¡","&iexcl;")

_CADENA := STRTRAN(_CADENA,"á","&aacute;")
_CADENA := STRTRAN(_CADENA,"é","&eacute;")
_CADENA := STRTRAN(_CADENA,"í","&iacute;")
_CADENA := STRTRAN(_CADENA,"ó","&oacute;")
_CADENA := STRTRAN(_CADENA,"ú","&uacute;")

_CADENA := STRTRAN(_CADENA,"Á","&Aacute;")
_CADENA := STRTRAN(_CADENA,"É","&Eacute;")
_CADENA := STRTRAN(_CADENA,"Í","&Iacute;")
_CADENA := STRTRAN(_CADENA,"Ó","&Oacute;")
_CADENA := STRTRAN(_CADENA,"Ú","&Uacute;")

_CADENA := STRTRAN(_CADENA,"ü","&uuml;")
_CADENA := STRTRAN(_CADENA,"Ü","&Uuml;")


/*
Carácter   Entidad HTML    Carácter   Entidad HTML
á  &aacute;    Á  &Aacute;
é  &eacute;    É  &Eacute;
í  &iacute;    Í  &Iacute;
ó  &oacute;    Ó  &Oacute;
ú  &uacute;    Ú  &Uacute;
ü  &uuml;      Ü  &Uuml;
ñ  &ntilde;    Ñ  &Ntilde;
¡  &iexcl;     ¿  &iquest;
*/



RETURN _CADENA


En esta rutina evito caracteres que pueden romper la estructura del xml

saludos
Visite Chiapas, el paraiso de México.
Posts: 537
Joined: Mon Jan 16, 2006 03:42 PM
Re: TRANFORMAR CARACTER ESPECIALES DE XML
Posted: Mon Nov 09, 2015 12:21 PM

eres muy amable, a sido de gran ayuda

saludos

Posts: 989
Joined: Thu Nov 24, 2005 03:01 PM
Re: TRANFORMAR CARACTER ESPECIALES DE XML
Posted: Mon Nov 09, 2015 03:08 PM
Ampliando la respuesta del compañero Devtuxla, se puede hacer un poco más rápido usando las nuevas funciones de Harbour. Están ahí pero no nos enteramos!

Extraído del Changelog.txt de Harbour

Code (fw): Select all Collapse
2013-02-15 01:53 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl)
  * harbour/include/harbour.hbx
  * harbour/src/rtl/Makefile
  + harbour/src/rtl/strxchg.c
    + added new PRG function:
         hb_StrReplace( <cString>, <cSource> | <acSource> | <hReplace>, ;
                                   <cDest> | <acDest> ] ) -> <cResult>
      This function allows to easy replace different substrings in
      given string.
      If 2-nd is string then each character in <cString> which exists
      in <cSource> at <n> position is replaced by corresponding character
      at <n> position in <cDest> or string from <acDest>[ <n> ]
      If 2-nd parameter is array then each <cString> substring which exists
      in <acSource> at <n> position is replaced by corresponding character
      at <n> position in <cDest> or string from <acDest>[ <n> ].
      If <n> is longer then LEN() of <cDest> or <acDest> then given
      character/substring is removed from result.
      This function should help in code which wrongly uses repeated
      StrTran() calls all regex which can change also substituted values.
      Examples:
         // encode XML value
         cXmlText := hb_StrReplace( cText, "<>&", { "<", ">", "&amp;" } )
         // now decode it to raw text
         cText := hb_StrReplace( cXmlText, { "<", ">", "&amp;" }, "<>&" )
         // strip all digits from string
         cNoDigit := hb_StrReplace( cText, "0123456789" )
         // extract all digits from string
         cDigits := hb_StrReplace( cText, cNoDigit )
         // convert chosen letters to upper case
         ? hb_StrReplace( "hello world, "hlwd", "HLWD" )


Es decir que en el caso que comentas todo se reduce a :

Code (fw): Select all Collapse
cTexto:= hb_StrReplace( cTexto, '&<>"´', { "&amp;", "<", ">", "&quot;", "&apos;" } )


Este Prezemeck es un genio!
Saludos
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
Posts: 990
Joined: Wed Oct 19, 2005 02:17 PM
Re: TRANFORMAR CARACTER ESPECIALES DE XML
Posted: Mon Nov 09, 2015 08:11 PM

Carlos,

muchas gracias por la información. Estaremos más atentos al fichero changelog de Harbour.

Saludos

Posts: 336
Joined: Wed May 16, 2007 09:40 PM
Re: TRANFORMAR CARACTER ESPECIALES DE XML
Posted: Wed Nov 11, 2015 07:35 PM
Carlos Mora wrote:Ampliando la respuesta del compañero Devtuxla, se puede hacer un poco más rápido usando las nuevas funciones de Harbour. Están ahí pero no nos enteramos!

Extraído del Changelog.txt de Harbour

Code (fw): Select all Collapse
2013-02-15 01:53 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl)
  * harbour/include/harbour.hbx
  * harbour/src/rtl/Makefile
  + harbour/src/rtl/strxchg.c
    + added new PRG function:
         hb_StrReplace( <cString>, <cSource> | <acSource> | <hReplace>, ;
                                   <cDest> | <acDest> ] ) -> <cResult>
      This function allows to easy replace different substrings in
      given string.
      If 2-nd is string then each character in <cString> which exists
      in <cSource> at <n> position is replaced by corresponding character
      at <n> position in <cDest> or string from <acDest>[ <n> ]
      If 2-nd parameter is array then each <cString> substring which exists
      in <acSource> at <n> position is replaced by corresponding character
      at <n> position in <cDest> or string from <acDest>[ <n> ].
      If <n> is longer then LEN() of <cDest> or <acDest> then given
      character/substring is removed from result.
      This function should help in code which wrongly uses repeated
      StrTran() calls all regex which can change also substituted values.
      Examples:
         // encode XML value
         cXmlText := hb_StrReplace( cText, "<>&", { "<", ">", "&amp;" } )
         // now decode it to raw text
         cText := hb_StrReplace( cXmlText, { "<", ">", "&amp;" }, "<>&" )
         // strip all digits from string
         cNoDigit := hb_StrReplace( cText, "0123456789" )
         // extract all digits from string
         cDigits := hb_StrReplace( cText, cNoDigit )
         // convert chosen letters to upper case
         ? hb_StrReplace( "hello world, "hlwd", "HLWD" )


Es decir que en el caso que comentas todo se reduce a :

Code (fw): Select all Collapse
cTexto:= hb_StrReplace( cTexto, '&<>"´', { "&amp;", "<", ">", "&quot;", "&apos;" } )


Este Prezemeck es un genio!


y como se llamaria esta funcion en xharbour...
Fivewin-Xharbour 24.09, Iquique, Chile

Continue the discussion