TRANFORMAR CARACTER ESPECIALES DE XML

Post Reply
jbrita
Posts: 425
Joined: Mon Jan 16, 2006 3:42 pm

TRANFORMAR CARACTER ESPECIALES DE XML

Post by jbrita »

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

EJEMPLO:
PINTURA B&W AFECTO

DEBE QUEDAR ASI

PINTURA B&W AFECTO

salduos
devtuxtla
Posts: 392
Joined: Tue Jul 29, 2008 1:55 pm

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Post by devtuxtla »

Hola

Esta rutina la uso , espero te sirva :

Code: Select all

**---------------------------------------------------------------------------**
FUNCTION TRANS_VALIDACADENA(_CADENA)
**---------------------------------------------------------------------------**
LOCAL X:=LEN(_CADENA)

_CADENA := STRTRAN(_CADENA,"&","&")
_CADENA := STRTRAN(_CADENA,">",">")
_CADENA := STRTRAN(_CADENA,"<","&lt")
_CADENA := STRTRAN(_CADENA,"|","")
_CADENA := STRTRAN(_CADENA,'"',""")
_CADENA := STRTRAN(_CADENA,"'",""")
_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.
jbrita
Posts: 425
Joined: Mon Jan 16, 2006 3:42 pm

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Post by jbrita »

eres muy amable, a sido de gran ayuda

saludos
Carlos Mora
Posts: 988
Joined: Thu Nov 24, 2005 3:01 pm
Location: Madrid, España

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Post by Carlos Mora »

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: Select all

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, "<>&", { "<", ">", "&" } )
         // now decode it to raw text
         cText := hb_StrReplace( cXmlText, { "<", ">", "&" }, "<>&" )
         // 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: Select all

cTexto:= hb_StrReplace( cTexto, '&<>"´', { "&", "<", ">", """, "&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"
User avatar
Baxajaun
Posts: 853
Joined: Wed Oct 19, 2005 2:17 pm
Location: Gatika. Bizkaia

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Post by Baxajaun »

Carlos,

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

Saludos
elmoiquique
Posts: 257
Joined: Wed May 16, 2007 9:40 pm
Location: Iquique Chile

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Post by elmoiquique »

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: Select all

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, "<>&", { "<", ">", "&" } )
         // now decode it to raw text
         cText := hb_StrReplace( cXmlText, { "<", ">", "&" }, "<>&" )
         // 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: Select all

cTexto:= hb_StrReplace( cTexto, '&<>"´', { "&", "<", ">", """, "&apos;" } )
 
Este Prezemeck es un genio!
y como se llamaria esta funcion en xharbour...
Fivewin 11.07
Post Reply