Reputation: 113
How do I replace line breaks within a specific XML node?
E.g.: ^J
to <br/>
within <content:encoded></content:encoded>
Sample of the code:
<content:encoded><![CDATA[
Communities across the Northwest Territories have harvested a bumper crop
of produce ranging from potatoes and carrots to spinach and lettuce thanks to
dedicated community gardeners and the Government of the Northwest Territories’ (GNWT).
]></content:encoded>
Upvotes: 0
Views: 309
Reputation: 2597
I think the solution is to visually select the lines, then :s#\n#<br/>\r#g
- it will replace the line endings to <br/>
.
If you want to replace it in every inner tag, then you shuld use:
:g /<content:encoded>/;/\/content:encoded/ s#\n#<br/>\r#g
Upvotes: 0
Reputation: 7678
You could use a macro:
qq
start recording/content:encoded<cr>
go to next tag. Note <cr>
means hit enter.vit
visually select in tag:'<,'>s/\n\s*/<br\/>/g
replace all newlines followed by any number of spaces with <br/>
. Note the '<,'>
will automatically be added by vim.q
stop recording5@q
repeat 5 times.It's probably possible using regex too but my regex foo isnt up to that.
Upvotes: 0
Reputation: 241968
Use proper XML handling tool. For example, xsh:
open file.xml ;
for my $text in //content:encoded/text() {
my $lines = xsh:split("\n", $text) ;
for $lines {
my $t := insert text (.) before $text ;
insert element br before $text ;
}
delete $text;
}
save :b ;
Upvotes: 0
Reputation: 172638
If there's only one such range, the following will do:
/<content:encoded>/,/<\/content:encoded>/s#$#<br/>#
This executes a :substitute
command over the range delimited by the (opening / closing) tags. The example adds the <br/>
tag, if you want to condense everything into one line, search for \n
instead of $
(\n\s*
to also remove the indent).
If there are multiple such tags that you want to replace, prepend :global
to the command. This will execute the substitute (this time from the current line containing the start tag to the next end tag) for all tags found in the buffer (which you can again influence by prepending a different range, e.g. 1,100global
).
Upvotes: 1