A practical example of parsing a specific HTML section and applying transformations only to the elements within it.

ID: 940

				
					using uCalcSoftware;

var uc = new uCalc();
// Note the change in section/div/h2
var t = uc.NewTransformer();

// The parent rule will find the <section> block and make its content available to a local transformer.
// StatementSensitive(false) is needed so the multiline content is captured.
var parentRule = t.Pattern("<section>{body}</section>");
parentRule.StatementSensitive = false;

// Get the local transformer for the <section> block.
var section_t = parentRule.LocalTransformer;

// These rules will ONLY run on the content inside the <section> tag.
section_t.FromTo("<h2>{text}</h2>", "<h1>====> {@Eval: UCase(text)} <====</h1>");
section_t.FromTo("<p>{text}</p>", "<p>SELECTED: {text}</p>");

var sourceHtml =
"""

<div>
  <h2>Article One</h2>
  <p>This is NOT in the section.</p>
</div>

<section>
  <div>
    <h2>Article Two</h2>
    <p>This one IS inside the section.</p>
  </div>
</section>

""";

t.Text = sourceHtml;
t.Transform();
Console.WriteLine(t.Text);
				
			
<div>
  <h2>Article One</h2>
  <p>This is NOT in the section.</p>
</div>

<section>
  <div>
    <h1>====> ARTICLE TWO <====</h1>
    <p>SELECTED: This one IS inside the section.</p>
  </div>
</section>
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Note the change in section/div/h2
   auto t = uc.NewTransformer();

   // The parent rule will find the <section> block and make its content available to a local transformer.
   // StatementSensitive(false) is needed so the multiline content is captured.
   auto parentRule = t.Pattern("<section>{body}</section>");
   parentRule.StatementSensitive(false);

   // Get the local transformer for the <section> block.
   auto section_t = parentRule.LocalTransformer();

   // These rules will ONLY run on the content inside the <section> tag.
   section_t.FromTo("<h2>{text}</h2>", "<h1>====> {@Eval: UCase(text)} <====</h1>");
   section_t.FromTo("<p>{text}</p>", "<p>SELECTED: {text}</p>");

   auto sourceHtml =
   R"(
<div>
  <h2>Article One</h2>
  <p>This is NOT in the section.</p>
</div>

<section>
  <div>
    <h2>Article Two</h2>
    <p>This one IS inside the section.</p>
  </div>
</section>
)";

   t.Text(sourceHtml);
   t.Transform();
   cout << t.Text() << endl;
}
				
			
<div>
  <h2>Article One</h2>
  <p>This is NOT in the section.</p>
</div>

<section>
  <div>
    <h1>====> ARTICLE TWO <====</h1>
    <p>SELECTED: This one IS inside the section.</p>
  </div>
</section>
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Note the change in section/div/h2
      Dim t = uc.NewTransformer()
      
      '// The parent rule will find the <section> block and make its content available to a local transformer.
      '// StatementSensitive(false) is needed so the multiline content is captured.
      Dim parentRule = t.Pattern("<section>{body}</section>")
      parentRule.StatementSensitive = false
      
      '// Get the local transformer for the <section> block.
      Dim section_t = parentRule.LocalTransformer
      
      '// These rules will ONLY run on the content inside the <section> tag.
      section_t.FromTo("<h2>{text}</h2>", "<h1>====> {@Eval: UCase(text)} <====</h1>")
      section_t.FromTo("<p>{text}</p>", "<p>SELECTED: {text}</p>")
      
      Dim sourceHtml =
      "
<div>
  <h2>Article One</h2>
  <p>This is NOT in the section.</p>
</div>

<section>
  <div>
    <h2>Article Two</h2>
    <p>This one IS inside the section.</p>
  </div>
</section>
"
      
      t.Text = sourceHtml
      t.Transform()
      Console.WriteLine(t.Text)
   End Sub
End Module
				
			
<div>
  <h2>Article One</h2>
  <p>This is NOT in the section.</p>
</div>

<section>
  <div>
    <h1>====> ARTICLE TWO <====</h1>
    <p>SELECTED: This one IS inside the section.</p>
  </div>
</section>