Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9041217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:11:12+00:00 2026-06-16T10:11:12+00:00

I have a HTML document that has a structure like this: <li class=indent1>(something) <li

  • 0

I have a HTML document that has a structure like this:

<li class="indent1">(something)
  <li class="indent2">(something else)</li>
  <li class="indent2">(something else)
    <li class="indent3">(another sublevel)</li>
  </li>
  <li class="indent2">(something else)</li>
</li>

What I need to do is wrap these LI tags in OL tags. There are numerous lists such as these throughout the document. The HTML needs to look as follows:

<ol>
  <li>(something)
    <ol>
      <li>(something else)</li>
      <li>(something else)
        <ol>
          <li>(another sublevel)</li>
        </ol>
      </li>
      <li>(something else)</li>
    </ol>
  </li>
</ol>

How might I go about doing this in Nokogiri? Many thanks in advance.

Edit:

Here’s an example of the HTML as it is in the original document. My script converted all of the P tags to LI tags.

  <p class="indent1"><i>a.</i> This regulation describes the Army Planning, Programming,
  Budgeting, and Execution System (PPBES). It explains how an integrated Secretariat and
  Army Staff, with the full participation of major Army commands (MACOMs), Program
  Executive Offices (PEOs), and other operating agencies--</p>

  <p class="indent2">(1) Plan, program, budget, and then allocate and manage approved
  resources.</p>

  <p class="indent2">(2) Provide the commanders in chief (CINCs) of United States unified
  and specified commands with the best mix of Army forces, equipment, and support
  attainable within available resources.</p>

  <p class="indent1"><i>b.</i> The regulation assigns responsibilities and describes
  policy and procedures for using the PPBES to:</p>

Indent 1 classes mean a first-level list item, indent 2 means second level, etc. I need these indent classes converted into proper ordered lists.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:11:13+00:00Added an answer on June 16, 2026 at 10:11 am

    The following solution works by looping through each <li> in the document and either:

    • If there is is no preceding <ol>, swap the <li> with a new one and then put the <li> inside there.
    • If there is an immediately preceding <ol>, move this <li> into it.
    document.css('li').each do |li|
      if li.at_xpath('preceding-sibling::node()[not(self::text()[not(normalize-space())])][1][self::ol]')
        li.previous_element << li
      else
        li.replace('<ol/>').first << li
      end
    end
    

    Here it is, tested:

    require 'nokogiri'
    
    # Use XML instead of HTML fragment due to problems with XPath
    fragment = Nokogiri::XML.fragment '
      <li>List 1
        <li>List 1a</li>
        <li>List 1b
          <li>List 1bi</li>
        </li>
        <li>List 1c</li>
        New List
        <li>New List 1a</li>
      </li>
      <p>Break 1</p>
      <li>List 2a</li>
      <li>List 2b</li>
      <p>Break 2</p>
      <li>List 3 <li>List 3a</li></li>
    '
    
    fragment.css('li').each do |li|
      # Complex test to see if the preceding element is an <ol> and there's no non-empty text the li and it
      # See http://stackoverflow.com/q/14045519/405017
      if li.at_xpath('preceding-sibling::node()[not(self::text()[not(normalize-space())])][1][self::ol]')
        li.previous_element << li
      else
        li.replace('<ol/>').first << li
      end
    end
    
    puts fragment   # I've normalized the whitespace in the output to make it clear
    #=> <ol>
    #=>   <li>List 1
    #=>     <ol>
    #=>       <li>List 1a</li>
    #=>       <li>List 1b
    #=>         <ol>
    #=>           <li>List 1bi</li>
    #=>         </ol>
    #=>       </li>
    #=>       <li>List 1c</li>
    #=>     </ol>
    #=>     New List
    #=>     <ol><li>New List 1a</li></ol>
    #=>   </li>
    #=> </ol>
    #=> <p>Break 1</p>
    #=> <ol>
    #=>   <li>List 2a</li>
    #=>   <li>List 2b</li>
    #=> </ol>
    #=> <p>Break 2</p>
    #=> <ol>
    #=>   <li>List 3
    #=>     <ol>
    #=>       <li>List 3a</li>
    #=>     </ol>
    #=>   </li>
    #=> </ol>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a html document that has this structure: https://i.stack.imgur.com/zrOq1.png <- sorry for link,
I have an iframe that looks like this: <iframe id=iframe2 ...> #document <html>...</html> </iframe>
I have a large HTML document which has roughly this structure: <div id=a> <!--
I have a HTML structure like this:- <article id=a_post class=a_post> <div id=thumbnail> <img id=shine
I have XML tag that has the content which is HTML document. <xml-tag> <!--
I have html structure like below: <div id=banner-menu-holder> <div> <div class=avalible> <div class=items-holder> <div>
Let's say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword>
I have a page with an iframe that has an html document within it.
I have an XML document that has a TextBlock that contains HTML code. <TextBlock>
I am trying to create an HTML document that should look something like Some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.