title: Translating malformed HTML to hierarchical XML
body: <p>I am currently looking for a way in Perl to write the following output in XML files</p>
  <ul>
  <li><p><code>h1</code> Is the parent level</p>
  </li>
  <li><p><code>h2</code> is the child level of <code>h1</code></p>
  </li>
  <li><p><code>h3</code> is a child level of <code>h2</code> (or a subchild of <code>h1</code>) etc.</p>
  </li>
  </ul>
  <h3>Example input</h3>
  <pre class="lang-html prettyprint-override"><code>&lt;h1&gt;1 Top level heading
  Para text 1
  Para text 2
  &lt;h2&gt;1.1 Sub level heading
  Para text 3
  Para text 4
  &lt;h3&gt;1.1.1 Sub sub level heading
  Para text 5
  Para text 6
  &lt;h2&gt;Sub level heading 2
  Para text 7
  Para text 8
  &lt;h1&gt;Top level heading
  Para text 1
  Para text 2
  </code></pre>
  <h1>Required output</h1>
  <pre class="lang-xml prettyprint-override"><code>&lt;h1&gt;
   &lt;label&gt;1&lt;/label&gt;
   &lt;title&gt;Top level heading&lt;/title&gt;
   &lt;p&gt;Para text 1&lt;/p&gt;
   &lt;p&gt;Para text 2&lt;/p&gt;
   
   &lt;h2&gt;
    &lt;label&gt;1.1&lt;/label&gt;
    &lt;title&gt;Sub level heading&lt;/title&gt;
    &lt;p&gt;Para text 3&lt;/p&gt;
    &lt;p&gt;Para text 4&lt;/p&gt;
  
    &lt;h3&gt;
      &lt;label&gt;1.1&lt;/label&gt;
      &lt;title&gt;Sub sub level heading&lt;/title&gt;
      &lt;p&gt;Para text 5&lt;/p&gt;
      &lt;p&gt;Para text 6&lt;/p&gt;
    &lt;/h3&gt;
   &lt;/h2&gt;
  
   &lt;h2&gt;Sub level heading (no number prefix)
    &lt;p&gt;Para text 7&lt;/p&gt;
    &lt;p&gt;Para text 8&lt;/p&gt;
   &lt;/h2&gt;
  &lt;/h1&gt;
  
  &lt;h1&gt;Top level heading (no number prefix)
  &lt;p&gt;Para text 9&lt;/p&gt;
  &lt;p&gt;Para text 10&lt;/p&gt;
  &lt;/h1&gt;
  </code></pre>
  <p>I tried a lot but found no logic to achieve this.</p>
  <p>Could someone help me to get started?</p>
  <h3>Update</h3>
  <p><strong>@Borodin's</strong> code works well based on the above input snippet, but my actual requirement is as follows:</p>
  <h3>Input.txt</h3>
  <pre class="lang-none prettyprint-override"><code>&lt;art&gt;Ärticle Title
  &lt;smry&gt;1 Summåry
   Summary paragragh 1...
   Summary paragragh 2...
  &lt;/smry&gt;
  &lt;subjg&gt;Subject Group Title
   subject 1; subject 2; subject 3
  &lt;/subjg&gt;
  
  &lt;h1&gt;1 Top level heading
    Para text 1
    &lt;img gr1.jpg&gt;
    Para text 2
  
    &lt;h2&gt;1.1 Sub level heading
      Para text 3
      Para text 4
      &lt;img gr2.jpg&gt;
  
    &lt;h2&gt;1.2 Sub level heading
      Para text 5
      Para text 6
  
     &lt;h3&gt;1.1.1 Sub sub level heading
       Para text 7
       &lt;fcap&gt;Label 1: Text...
       &lt;grp line1.png&gt;
       Para text 8
  
     &lt;h3&gt;1.1.2 Sub sub level heading
       Para text 9
       Para text 10
    &lt;h2&gt;Sub level heading
      &lt;fcap&gt;Text only...
      &lt;grp line2.png&gt;
      Para text 11
      Para text 12
  
  &lt;h1&gt;Top level heading
   Para text 13
   Para text 14
  
    &lt;h2&gt;Sub level heading
      Para text 15
      Para text 16
  
  &lt;blst&gt;Books
   [1] Book name 1...
   [2] Book name 2...
   [3] Book name 3...
  &lt;/blst&gt;
  
  &lt;art&gt;
  ...
  &lt;art&gt;
  ...
  </code></pre>
  <h3>Required Output.xml</h3>
  <pre class="lang-none prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;article&gt;
    &lt;front&gt;
      &lt;title&gt;&amp;#x00C4;rticle Title&lt;/title&gt;
      &lt;summary&gt;
        &lt;label&gt;1&lt;/label&gt;
        &lt;title&gt;Summ&amp;#x00E5;ry&lt;/title&gt;
        &lt;p&gt;Summary paragragh 1...&lt;/p&gt;
        &lt;p&gt;Summary paragragh 2...&lt;/p&gt;
      &lt;/summary&gt;
      &lt;subj-group&gt;
        &lt;title&gt;Subject Group Title&lt;/title&gt;
        &lt;sub&gt;subject 1&lt;/sub&gt;
        &lt;sub&gt;subject 2&lt;/sub&gt;
        &lt;sub&gt;subject 3&lt;/sub&gt;
      &lt;/subj-group&gt;
    &lt;/front&gt;
    &lt;body&gt;
      &lt;h1 id=&quot;s1&quot;&gt;
        &lt;label&gt;1&lt;/label&gt;
        &lt;title&gt;Top level heading&lt;/title&gt;
        &lt;p&gt;Para text 1&lt;/p&gt;
        &lt;img src=&quot;gr1.jpg&quot; id=&quot;gr1&quot;/&gt;
        &lt;p&gt;Para text 2&lt;/p&gt;
        &lt;h2 id=&quot;s1a&quot;&gt;
          &lt;label&gt;1.1&lt;/label&gt;
          &lt;title&gt;Sub level heading&lt;/title&gt;
          &lt;p&gt;Para text 3&lt;/p&gt;
          &lt;p&gt;Para text 4&lt;/p&gt;
          &lt;img src=&quot;gr2.jpg&quot; id=&quot;gr2&quot;/&gt;
        &lt;/h2&gt;
        &lt;h2 id=&quot;s1b&quot;&gt;
          &lt;label&gt;1.2&lt;/label&gt;
          &lt;title&gt;Sub level heading&lt;/title&gt;
          &lt;p&gt;Para text 5&lt;/p&gt;
          &lt;p&gt;Para text 6&lt;/p&gt;
          &lt;h3 id=&quot;s1b1&quot;&gt;
            &lt;label&gt;1.1.1&lt;/label&gt;
            &lt;title&gt;Sub sub level heading&lt;/title&gt;
            &lt;p&gt;Para text 7&lt;/p&gt;
            &lt;figure id=&quot;grp1&quot;&gt;
              &lt;label&gt;Label 1:&lt;/label&gt;
              &lt;cap&gt;&lt;p&gt;Text...&lt;/p&gt;&lt;/cap&gt;
              &lt;graphic src=&quot;line1.png&quot;/&gt;
            &lt;/figure&gt;
            &lt;p&gt;Para text 8&lt;/p&gt;
          &lt;/h3&gt;
          &lt;h3 id=&quot;s1b2&quot;&gt;
            &lt;label&gt;1.1.2&lt;/label&gt;
            &lt;title&gt;Sub sub level heading&lt;/title&gt;
            &lt;p&gt;Para text 9&lt;/p&gt;
            &lt;p&gt;Para text 10&lt;/p&gt;
          &lt;/h3&gt;
        &lt;/h2&gt;
        &lt;h2 id=&quot;s1c&quot;&gt;
          &lt;title&gt;Sub level heading 2&lt;/title&gt;
          &lt;figure id=&quot;grp2&quot;&gt;
            &lt;cap&gt;&lt;p&gt;Text only...&lt;/p&gt;&lt;/cap&gt;
            &lt;graphic src=&quot;line2.png&quot;/&gt;
          &lt;/figure&gt;
          &lt;p&gt;Para text 11&lt;/p&gt;
          &lt;p&gt;Para text 12&lt;/p&gt;
        &lt;/h2&gt;
      &lt;/h1&gt;
      &lt;h1 id=&quot;s2&quot;&gt;
        &lt;title&gt;Top level heading&lt;/title&gt;
        &lt;p&gt;Para text 13&lt;/p&gt;
        &lt;p&gt;Para text 14&lt;/p&gt;
        &lt;h2 id=&quot;s2a&quot;&gt;
          &lt;title&gt;Sub level heading 2&lt;/title&gt;
          &lt;p&gt;Para text 15&lt;/p&gt;
          &lt;p&gt;Para text 16&lt;/p&gt;
        &lt;/h2&gt;
      &lt;/h1&gt;
    &lt;/body&gt;
    &lt;back&gt;
      &lt;booklist&gt;
        &lt;title&gt;Books&lt;/title&gt;
        &lt;bookname id=&quot;b1&quot;&gt;&lt;l&gt;[1]&lt;/l&gt;&lt;t&gt;Book name 1...&lt;/t&gt;&lt;/bookname&gt;
        &lt;bookname id=&quot;b2&quot;&gt;&lt;l&gt;[2]&lt;/l&gt;&lt;t&gt;Book name 2...&lt;/t&gt;&lt;/bookname&gt;
        &lt;bookname id=&quot;b3&quot;&gt;&lt;l&gt;[3]&lt;/l&gt;&lt;t&gt;Book name 3...&lt;/t&gt;&lt;/bookname&gt;
      &lt;/booklist&gt;
    &lt;/back&gt;
  &lt;/article&gt;
  </code></pre>
  <p>Could someone help me on this?</p>