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