DaemonForums  

Go Back   DaemonForums > Miscellaneous > Guides

Guides All Guides and HOWTO's.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 3rd December 2009
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,128
Default A script to escape the reserved XML characters

For the reason see http://en.wikipedia.org/wiki/XML#Escaping

Code:
#!/bin/sh

# --- replace
# '&' with '&'      :       's~\&~\&~g'
# '<' with '&lt;'       :       's~<~\&lt;~g'
# '>' with '&gt;'       :       's~>~\&gt;~g'
# '"' with '&quot'      :       's~\"~\&quot;~g'

# --- First replace all existing '&' signs.
# --- Prevents an original '<' first changed into '&lt;' ending up as '&amp;lt;'

sed -e 's~&~\&amp;~g' -e 's~<~\&lt;~g'  -e  's~>~\&gt;~g'

#  's~&~\&amp;~g'
#
#       s               search and replace
#       ~               our custom delimiter indicating start of search pattern
#       &               a '&' 
#       ~               end of search pattern, start of replacement
#
#       \&              a "&" escaped with "\" ( in replacement "&" means: matched pattern )
#       amp             followed by a 'a', 'm' and 'p'
#       ;               and a ';'
#
#       ~               end of replacement
#       g               the search and replace globally: don't stop at first occurrence,
#                       but check the whole line
A complete file could be converted with:
Code:
$ esc-xml <infile.txt >outfile.xml
A example of using 'esc-xml' from within vi:
  • enter command mode with ESC
  • type a ':'
  • the line range
  • a '!' followed by the name of the script

In the follpwing screenshot the script has been pasted into a text file and we will have it operate on itself with:
Code:
37,62!esc-xml
Code:
     36 <code>
     37 #!/bin/sh
     38 
     39 # --- replace
     40 # '&' with '&amp;'      :       's~\&~\&amp;~g'
     41 # '<' with '&lt;'       :       's~<~\&lt;~g'
     42 # '>' with '&gt;'       :       's~>~\&gt;~g'
     43 # '"' with '&quot'      :       's~\"~\&quot;~g'
     44 
     45 # --- First replace all existing '&' signs.
     46 # --- Prevents an original '<' first changed into '&lt;' ending up as '&amp;lt;'
     47 
     48 sed -e 's~&~\&amp;~g' -e 's~<~\&lt;~g'  -e  's~>~\&gt;~g'
     49 
     50 #  's~&~\&amp;~g'
     51 #
     52 #       s               search and replace
     53 #       ~               our custom delimiter indicating start of search pattern
     54 #       &               a '&'
     55 #       ~               end of search pattern, start of replacement
     56 #
     57 #       \&              a "&" escaped with "\" ( in replacement "&" means: matched patt
ern )
     58 #       amp             followed by a 'a', 'm' and 'p'
     59 #       ;               and a ';'
     60 #
     61 #       ~               end of replacement
     62 #       g               the search and replace global
     63 </code>
     64
~
:37,62!esc-xml
After pressing 'ENTER' the result is:
Code:
     36 <code>
     37 #!/bin/sh
     38
     39 # --- replace
     40 # '&amp;' with '&amp;amp;'      :       's~\&amp;~\&amp;amp;~g'
     41 # '&lt;' with '&amp;lt;'       :       's~&lt;~\&amp;lt;~g'
     42 # '&gt;' with '&amp;gt;'       :       's~&gt;~\&amp;gt;~g'
     43 # '"' with '&amp;quot'      :       's~\"~\&amp;quot;~g'
     44
     45 # --- First replace all existing '&amp;' signs.
     46 # --- Prevents an original '&lt;' first changed into '&amp;lt;' ending up as '&amp;amp;lt;'
     47
     48 sed -e 's~&amp;~\&amp;amp;~g' -e 's~&lt;~\&amp;lt;~g'  -e  's~&gt;~\&amp;gt;~g'
     49
     50 #  's~&amp;~\&amp;amp;~g'
     51 #
     52 #       s               search and replace
     53 #       ~               our custom delimiter indicating start of search pattern
     54 #       &amp;               a '&amp;'
     55 #       ~               end of search pattern, start of replacement
     56 #
     57 #       \&amp;              a "&amp;" escaped with "\" ( in replacement "&amp;" means: matched
pattern )
     58 #       amp             followed by a 'a', 'm' and 'p'
     59 #       ;               and a ';'
     60 #
     61 #       ~               end of replacement
     62 #       g               the search and replace global
     63 </code>
26 lines added; 26 lines deleted
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
Reply

Tags
vi, xml

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remap colour escape codes bsdperson FreeBSD General 2 14th September 2009 10:52 AM
Input foreign characters under X11 Beastie General software and network 5 30th August 2009 11:51 AM
escape single quote in sed gosha Programming 5 9th March 2009 10:22 AM
Username longer than 16 characters _hmp_ FreeBSD General 5 13th January 2009 10:01 AM
can't get out of twm - any escape? Lazy FreeBSD General 4 28th June 2008 07:26 PM


All times are GMT. The time now is 09:27 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick