简体   繁体   English

nokogiri:xml到html

[英]nokogiri: xml to html

I just want to do some straight conversion (almost just search and replace) but I'm having trouble just getting things to sit in place - I'm ending up with links out of place and duplicated content. 我只想进行一些直接转换(几乎只是搜索和替换),但在将事情放到原位时遇到了麻烦-我最终会出现链接到位和内容重复的情况。 I'm sure I'm doing something silly with my attempts at traversing the xml : ) 我确信遍历xml的尝试做得很愚蠢:

I'm trying with: 我正在尝试:

builder = Nokogiri::HTML::Builder.new do |doc|
 doc.html {
  doc.body {
   doc.div.wrapper! {
    doc.h1 "Short"

      xm.css('paragraph').each do |para|

        doc.h3.para(:id => para['number']) { doc.text para['number'] }

        doc.p.narrativeparagraph {

           xm.css('paragraph inner-section').each do |section|
              doc.span.innersection { doc.text section.content 

           xm.css('inner-section xref').each do |xref|
              doc.a(:href => "#" + xref['number']) { doc.text xref['number'] }
           end

           xm.css('paragraph inner-text').each do |innertext|
               doc.span.innertext { doc.text innertext.content }
           end

                } end #inner-section                 

                }

          end#end paragraph
        }#end wrapper
      }#end body
    }#end html
  end#end builder

on: 上:

<?xml version="1.0"?>

<looseleaf>

<paragraph number="1">
  <inner-section> blah one blah <xref number="link1location"></xref>
    <inner-text> blah two blah blah </inner-text>
     blah three
  </inner-section>
</paragraph>

<paragraph number="2">
<inner-section> blah four blah <xref number="link2location"></xref>
    <inner-text>blah five blah blah </inner-text>
         blah six
</inner-section>
</paragraph>

</looseleaf>

to create: 创造:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC- html40/loose.dtd">
<html>
<body>
<div id="wrapper">
<h1>Short</h1>
<h3 class="para" id="1">1</h3>
<p class="narrativeparagraph">
 <span class="innersection"> blah one blah <a href="#link1location">link1location</a>
 <span class="innertext"> blah two blah blah </span>
     blah three</span>
</p>

<h3 class="para" id="2">2</h3>
<p class="narrativeparagraph">
 <span class="innersection"> blah four blah <a ref="#link2location">link2location</a>
 <span class="innertext">blah five blah blah </span>
     blah six</span></p>

I've been trying all sorts of things to try to get this to work, the basic html structure comes out okay, but the children of the paragraphs are a mess - any help would be very much appreciated. 我一直在尝试各种方法来使它正常工作,基本的html结构都可以,但是这些段落的子项很烂-任何帮助将不胜感激。 Regards, Ritchie 问候,里奇

There are many ways to do this, but if you insist on the Builder way, I would make a function that translates <paragraph> to <p> . 有很多方法可以做到这一点,但是如果您坚持使用Builder方法,我将创建一个将<paragraph>转换为<p>的函数。

builder = Nokogiri::HTML::Builder.new do |doc|
  doc.html {
    doc.body {
      doc.div.wrapper! {
        doc.h1 "Short"
        xm.css('paragraph').each do |para|
          doc << translate_paragraph para.dup
        end #para
    }#end body
  }#end html
end#end builder

def translate_paragraph(p)
  # Change '<paragraph>' to '<p>'
  p.name = 'p'

  # Change '<innersection>' to '<span class='innersection'>'
  p.css('innersection').each { |tag|
    tag.name = 'span'
    tag['class'] = 'innersection'
  }

  # ...
end

Not perfect, but it works with Builder. 并不完美,但可以与Builder一起使用。

I would also consider XSLT, or traversing the HTML tree recursively and building from there. 我还将考虑XSLT,或者递归地遍历HTML树并从那里构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM