Ant replace text with special characters

It should be pretty simple. I want to open a file, find a string, and then replace that string with a string that has special characters:

<replace file="${src}/index.html" token="</copyright>"
value="</copyright> <legalnotice xml:id="DocLegalNotice" xml:base="../../../reusable-content/legal-notice.xml" xml:lang="">"/>

      

When I run this, the assembly doesn't like special characters:

The value of attribute "token" associated with an element type "replace"
must not contain the '<' character.

      

Do I need to escape every special character for "token" and "value", or is there an easier way?

+3


source to share


1 answer


Your first problem is that the ant file must be a well-formed XML file, and well-formed XML cannot have <

characters in the attribute values. Use instead &lt;

.

The second problem is that an attribute value delimited by double quote characters ( "

) cannot have unescaped nested double quote characters. Use instead '

.



<replace file="${src}/index.html" 
         token="&lt;/copyright>"
         value="&lt;/copyright> &lt;legalnotice xml:id='DocLegalNotice' xml:base='../../../reusable-content/legal-notice.xml' xml:lang=''>"/>

      

At least your ant file will be well-formed, but actually makes a substitute for an XML file, treating it as text, a bit hacked. Better would be to use an XML tool (like XSLT) to make such changes.

+3


source







All Articles