add attribute to element from value of next element
-
Hi community! Need your help. I have this code
<triggers> <request> <num>1</num> <lastname>xxx</lastname> <firstname>xxx</firstname> <middlename>xxx</middlename> <birthday>xxx</birthday> <birthplace>xxx</birthplace> <doctype>1</doctype> <docno>xxx</docno> <docdate>xxx</docdate> <docplace>xxx</docplace> <inn>xxx</inn> <pfno>xxx </pfno> <reason>xxx</reason> <reason_text>xxx </reason_text> <consent>xxx</consent> <consentdate>xxx</consentdate> <consentenddate>xxx</consentenddate> <admcode_inform>xxx</admcode_inform> <consent_owner>xxx </consent_owner> <contractno>xxx </contractno> </request> ......................... (many requests here) </triggers>
and i want it to look like this
<triggers> <request num="1"> <lastname>xxx</lastname> <firstname>xxx</firstname> ... </request>
so <num>value</num> goes to <request num=“value”>
any help would be really really appreciated.
-
@Anar-Movsumov
find/replace form; replace(?s)<request>\s*<num>(\d+)</num>(.*?</request>)
with<request id="\1">\2
.The very excellent regex manual on the reference website has some pages that may be of interest:
- single character matches on
\s
,.
, and\d
- capture groups and backreferences for why I wrapped
(\d+)
and(.+</request>)
in parentheses - search modifiers to understand how
(?s)
works - multiplying operators on why you need to use
.*?
and not.*
to match everything between the<num>
element and the remaining children of the<request>
tag.
- single character matches on