Page 1 of 1

TRANFORMAR CARACTER ESPECIALES DE XML

Posted: Fri Nov 06, 2015 2:26 pm
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

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Posted: Sat Nov 07, 2015 1:43 am
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

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Posted: Mon Nov 09, 2015 12:21 pm
by jbrita
eres muy amable, a sido de gran ayuda

saludos

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Posted: Mon Nov 09, 2015 3:08 pm
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!

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Posted: Mon Nov 09, 2015 8:11 pm
by Baxajaun
Carlos,

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

Saludos

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Posted: Wed Nov 11, 2015 7:35 pm
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...