I have a SGML file with multiple and elements. A chapter can stand alone on it's own, but a section must be inside a chapter. In a chapter there can be multiple sections. My problem is at the end of the multiple sections the chapter must have an end element . Right now it doesn't it just has the multiple section elements.
I've tried putting the document into an array and counting out the elements but that didn't work.
I tried adding before the next element but that left end elements that were in the wrong order.
I thought maybe treat it like an XML file and find the last child in the file then paste . But didn't know how to do it.
I'm sorry I'm really stumped on this so I don't have any code to post. I have no idea how to approach this.
I really appreciate all your help.
This is the document sample
<doc service="xT">
<body numcols="1">
<chapter id="chap1">
<para0><title></title></para0>
</chapter>
<chapter id="chap2"> <title>THEORY</title>
<section id="Thoery">
<title>theory Section</title>
<para0 verstatus="ver">
<title>Theory Para 0 </title>
<text>blah blah</text>
</para0>
</section>
<section id="Next section">
<title>title</title>
<para0>
<title>Title</title>
<text>blah blah</text>
</para0>
</section>
<section id="More sections">
<title>title</title>
<para0>
<title>Title</title>
<text>blah blah</text>
</para0>
</section>
<section id="section">
<title>title</title>
<para0>
<title>Title</title>
<text>blah blah</text>
</para0>
</section>
<chapter id="chap1">
<para0><title></title></para0>
</chapter>
<chapter id="chap1">
<para0><title></title></para0>
</chapter>
<chapter> <title>Chapter Title</title>
<section id="Section ID">
<title>Section Title</title>
<para0>
<title>Para0 Title</title>
<para>blah blah</para>
</para0>
</section>
<section id="Next section">
<title>title</title>
<para0>
<line>Title</line>
<text>blah blah</text>
</para0>
</section>
<ipbchap>
<tags></tags>
</ipbchap>
</body>
<rear>
<tags></tags>
</rear>
</doc>
Here is the expected results
<chapter id="chap1">
<para0><title></title></para0>
</chapter>
<chapter id="chap2"> <title>THEORY</title>
<section id="Thoery">
<title>theory Section</title>
<para0 verstatus="ver">
<title>Theory Para 0 </title>
<text>blah blah</text>
</para0>
</section>
<section id="Next section">
<title>title</title>
<para0>
<title>Title</title>
<text>blah blah</text>
</para0>
</section>
<section id="More sections">
<title>title</title>
<para0>
<title>Title</title>
<text>blah blah</text>
</para0>
</section>
<section id="section">
<title>title</title>
<para0>
<title>Title</title>
<text>blah blah</text>
</para0>
</section>
</chapter>
<chapter id="chap1">
<para0><title></title></para0>
</chapter>
<chapter id="chap1">
<para0><title></title></para0>
</chapter>
<chapter> <title>Chapter Title</title>
<section id="Section ID">
<title>Section Title</title>
<para0>
<title>Para0 Title</title>
<para>blah blah</para>
</para0>
</section>
<section id="Next section">
<title>title</title>
<para0>
<line>Title</line>
<text>blah blah</text>
</para0>
</section>
</chapter>
<ipbchap>
<tags></tags>
</ipbchap>
</body>
<rear>
<tags></tags>
</rear>
</doc>
This is the code that creates this file.
'Read all text of the Master Document
'and create a StringBuilder from it.
'All replacements will be done on the
'StringBuilder as it is more efficient
'than using Strings directly
Dim strMasterDoc = File.ReadAllText(existingMasterFilePath)
Dim newMasterFileBuilder As New StringBuilder(strMasterDoc)
'Create a regex with a named capture group.
'The name is 'EntityNumber' and captures just the
'entity digits for use in building the file name
Dim rx = New Regex("&" & Prefix & "_Ch(?<EntityNumber>\d+(?:-\d+)*)[;]")
Dim rxMatches = rx.Matches(strMasterDoc)
For Each match As Match In rxMatches
Dim entity = match.ToString
'Build the file name using the captured digits from the entity in the master file
Dim entityFileName = Prefix & $"_Ch{match.Groups("EntityNumber")}.sgm.bak"
Dim entityFilePath = Path.Combine(searchDir, entityFileName)
'Check if the entity file exists and use its contents
'to replace the entity in the copy of the master file
'contained in the StringBuilder
If File.Exists(entityFilePath) Then
Dim entityFileContents As String = File.ReadAllText(entityFilePath)
newMasterFileBuilder.Replace(entity, entityFileContents)
End If
Next
'write the processed contents of the master file to a different file
File.WriteAllText(newMasterFilePath, newMasterFileBuilder.ToString)
The issue is the code doesn't take in the final element because the element is empty .
So an approach I could take is to have these empty sections added to the document then remove them?