Responsive Ad Area

Share This Post

test

How to generate a different HTML format from a Nokogiri collection in Ruby

I’m a newbie working on a ruby script (requirement) that migrates a current HTML code and transforms it into a different HTML layout. So far I can get the information from the document using Nokogiri and XPath. The problem I am running into now is how to traverse the nodes retrieved with a loop in a similar fashion to how an array and a hash are traversed to generate the layout that I need.

Below is a sample of the original layout that I am trying to convert:

<ul id="nav">
    <li><a href="somelink1.html">Link 1</a> </li>
    <li>
        <a href="#">Link 2</a> 
        <ul>
            <li><a href="sublink1.html">Sublink 1</a></li>
            <li><a href="sublink2.html">Sublink 2</a></li>
        </ul>
    </li>
</ul>

The code below is what I have tried so far to get the result I’m looking for. The problem I have run into is that when it loops through the collection set it outputs all of the nodes in the new HTML tag each time a pass is made through the collection, rather than only outputting information at the current index.

require 'nokogiri'
source_file = Nokogiri.XML(open("navigation.inc"))
source_file = Nokogiri.XML(source_file.to_s.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: ''))

navigation = ""

if source_file.xpath("//ul[@id = 'nav']").length > 0

   navcontain = source_file.xpath("//ul[@id = 'nav']/li")

   navcontain.each do | child | 

   if child.xpath("//li and count(*) = 2")
        navigation = navigation + "<details>"
        child.xpath("//li/ul").each do | children | 
                 navigation = navigation + child.xpath("//li/a").to_s

         end #end child loop
        navigation = navigation + "</details>"
     else
        navigation = navigation + source_file.xpath("//ul[@id = 'nav']/li/a").to_s

     end #end conditional check
   end #end initial loop
end #end length check

puts navigation

This is an example of what the code above is currently doing:

<div id="nav">
    <details>
        <a href="somelink1.html">Link 1</a>
        <a href="#">Link 2</a> 
        <a href="sublink1.html">Sublink 1</a>
        <a href="sublink2.html">Sublink 2</a>
    </details>
    <details>
        <a href="somelink1.html">Link 1</a>
        <a href="#">Link 2</a> 
        <a href="sublink1.html">Sublink 1</a>
        <a href="sublink2.html">Sublink 2</a>
    </details>

</div>

The format that I am trying to accomplish through the transformation looks like the example below:

<div id="nav">
    <a href="somelink1.html">Link 1</a>

    <details>
        <summary>
            Link 2
        </summary>

        <a href="sublink1.html">Sublink 1</a>
        <a href="sublink2.html">Sublink 2</a>

    </details>

</div>

I believe part of the code works correctly as I am able to identify the total number of single and second-level link structures, I just cannot figure out how to translate the data to the final version I need. Thanks in advance for any help that can be provided.


How to generate a different HTML format from a Nokogiri collection in Ruby
How to generate a different HTML format from a Nokogiri collection in Ruby
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar