Community
    • Login

    add attribute to element from value of next element

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    2 Posts 2 Posters 170 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Anar MovsumovA
      Anar Movsumov
      last edited by Anar Movsumov

      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.

      Mark OlsonM 1 Reply Last reply Reply Quote 0
      • Mark OlsonM
        Mark Olson @Anar Movsumov
        last edited by Mark Olson

        @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.
        1 Reply Last reply Reply Quote 3
        • First post
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors