TRANFORMAR CARACTER ESPECIALES DE XML

TRANFORMAR CARACTER ESPECIALES DE XML

Postby jbrita » Fri Nov 06, 2015 2: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
jbrita
 
Posts: 486
Joined: Mon Jan 16, 2006 3:42 pm

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Postby devtuxtla » Sat Nov 07, 2015 1:43 am

Hola

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

_CADENA := STRTRAN(_CADENA,"&","&amp;")
_CADENA := STRTRAN(_CADENA,">","&gt;")
_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.
devtuxtla
 
Posts: 392
Joined: Tue Jul 29, 2008 1:55 pm

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Postby jbrita » Mon Nov 09, 2015 12:21 pm

eres muy amable, a sido de gran ayuda

saludos
jbrita
 
Posts: 486
Joined: Mon Jan 16, 2006 3:42 pm

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Postby Carlos Mora » Mon Nov 09, 2015 3: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: Select all  Expand view

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

cTexto:= hb_StrReplace( cTexto, '&<>"´', { "&amp;", "&lt;", "&gt;", "&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"
Carlos Mora
 
Posts: 988
Joined: Thu Nov 24, 2005 3:01 pm
Location: Madrid, España

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Postby Baxajaun » Mon Nov 09, 2015 8:11 pm

Carlos,

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

Saludos
User avatar
Baxajaun
 
Posts: 961
Joined: Wed Oct 19, 2005 2:17 pm
Location: Gatika. Bizkaia

Re: TRANFORMAR CARACTER ESPECIALES DE XML

Postby elmoiquique » Wed Nov 11, 2015 7: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: Select all  Expand view

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

cTexto:= hb_StrReplace( cTexto, '&<>"´', { "&amp;", "&lt;", "&gt;", "&quot;", "&apos;" } )
 


Este Prezemeck es un genio!


y como se llamaria esta funcion en xharbour...
Fivewin 11.07
elmoiquique
 
Posts: 297
Joined: Wed May 16, 2007 9:40 pm
Location: Iquique Chile


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 33 guests

cron