简体   繁体   中英

perl one-liner for XML encoding

Just like I can use echo 'one two' | perl -MURI::Escape -wlne 'print uri_escape $_' echo 'one two' | perl -MURI::Escape -wlne 'print uri_escape $_' to url-encode a string, how can I do something similar to encode a string to valid XML? (I'll always be using it on valid URLs, but I need to include them inside an XML tag).

The following will do for XML text:

 perl -MHTML::Entities -CS -pe'$_ = encode_entities($_, "&<")'

The following will do for XML attributes delimited by " :

 perl -MHTML::Entities -CS -pe'$_ = encode_entities($_, q{&<"})'

The following will do for XML attributes delimited by ' :

 perl -MHTML::Entities -CS -pe'$_ = encode_entities($_, "&<'\''")'

All together:

 perl -MHTML::Entities -CS -pe'$_ = encode_entities($_, q{&<>"'\''})'

I included > even though it's not necessary since most people do.

This program assumes the character set of the document into which the text will be inserted is Unicode, which means it assumes the document's encoding is UTF-8, UTF-16le, UTF-16be, UTF-32le or UTF-32be. If the document uses a different character set, Unicode characters not present in the character set will also need to be escaped.

This program assumes the input and output is UTF-8. (Your URI-encoding program assumes its input is UTF-8.)


But since you're talking about valid URLs, the following will do for XML text and for attributes delimited by " :

 perl -pe's/&/&amp;/g'

If the value could be used in as an attribute value delimited by ' , you'll need

 perl -pe"s/&/&amp;/g s/'/&apos;/g"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM