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
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login