Community
    • Login

    add attribute to element from value of next element

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    2 Posts 2 Posters 330 Views 1 Watching
    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 Offline
      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 Offline
        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

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