HTML Output from XML Problem

12 views
Skip to first unread message

ritchielee

unread,
Nov 24, 2009, 9:20:21 AM11/24/09
to nokogiri-talk
Hi

I have the following partial structure in some XML and I am using HTML
Builder for output.

<narrative-paragraph number="1"> text text text text text text
text text text </narrative-paragraph>

<narrative-paragraph number="2"> text text text text text text
text text <narrative-xref number="1"></narrative-xref> text text
text text text text text text </narrative-paragraph>

etc

The following code works to give me a heading and paragraph - but I
can't for the life of me figure out how to keep the links in situ -
they get stripped out of their location and bunch up at the end of
each paragraph. I figure this is how I have my loop placed, but other
than this I can't get them to output at all.

xmz = File.open("chapter.xml", "r")

xm = Nokogiri::XML.parse(xmz)

builder = Nokogiri::HTML::Builder.new do |doc|

doc.html {
doc.body {

xm.xpath('//narrative-paragraph').each do |para|

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

doc.p.paratext {doc.text para.content
xm.css('narrative-paragraph narrative-
xref').each do |link|
a(:href => "#" + link['number']) { doc.text
link['number'] }
end }
end

...

Current output
-------------------
<p class="heading" id="2">2</p>
<p class="paratext">text text text text text text text text
text text text text text text text text
<a href="#2">2</a></p>

Regards,
Ritchie

Mike Dalessio

unread,
Nov 24, 2009, 10:55:38 AM11/24/09
to nokogi...@googlegroups.com
Hi,

On Tue, Nov 24, 2009 at 9:20 AM, ritchielee <r...@northerncreation.com> wrote:
Hi

I have the following partial structure in some XML and I am using HTML
Builder for output.

<narrative-paragraph number="1"> text  text  text  text  text  text
text  text  text </narrative-paragraph>

<narrative-paragraph number="2"> text  text  text  text  text  text
text  text <narrative-xref number="1"></narrative-xref>  text  text
text  text  text  text  text  text </narrative-paragraph>

etc

The following code works to give me a heading and paragraph - but I
can't for the life of me figure out how to keep the links in situ -
they get stripped out of their location and bunch up at the end of
each paragraph. I figure this is how I have my loop placed, but other
than this I can't get them to output at all.

It's tough to figure out what exactly you're doing here. What are the exact contents of chapter.xml?

It would be really helpful if you posted a gist of a sample case that is entirely self-contained (including XML or HTML input), and a description of what your expected output is.
 

 xmz = File.open("chapter.xml", "r")

  xm = Nokogiri::XML.parse(xmz)

  builder = Nokogiri::HTML::Builder.new do |doc|

 doc.html {
       doc.body {

         xm.xpath('//narrative-paragraph').each do |para|

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

           doc.p.paratext {doc.text para.content
                     xm.css('narrative-paragraph narrative-
xref').each do |link|
                       a(:href => "#" + link['number']) { doc.text
link['number'] }
                     end }
           end

...

Current output
-------------------
<p class="heading" id="2">2</p>
<p class="paratext">text  text  text  text  text  text  text  text
text  text  text  text  text  text  text  text
<a href="#2">2</a></p>

Regards,
Ritchie

--

You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
To post to this group, send email to nokogi...@googlegroups.com.
To unsubscribe from this group, send email to nokogiri-tal...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nokogiri-talk?hl=en.





--
mike dalessio
mi...@csa.net

Message has been deleted

ritchielee

unread,
Nov 25, 2009, 1:11:49 PM11/25/09
to nokogiri-talk
Child elements are getting duplicated and bunched together at the end
of each narrative-paragraph. What I need to fathom is how to gather
the processed children up in para.content and not loop and duplicate
them at the end.

Current code & output:

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

doc.p.npt(:id => para['number']) { doc.text para
['number'] }
doc.p.np { doc.text para.content

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

xm.css('narrative-paragraph para-
text').each do |paratext|
doc.span.paratext { doc.text
paratext.content }
end

xm.css('narrative-paragraph legis-
cite').each do |legiscite|
doc.span.legiscite
{ doc.text legiscite.content }
end

}...

outputs...

<p class="npt" id="1.001">1.001</p>
<p class="np">
The
Legal Services Act 2007 has been enacted to establish a new Legal
Services Board as well as an Office for Legal Complaints and also
&ldquo;to make provision for, and in connection with, the regulation
of persons who carry on certain legal activities&rdquo;. A
Commencement No.1 and Transitory Provisions Order has been made (
SI 2008/222) bringi...
...Act set out the offences committed by persons carrying out
restricted legal activities without entitlement.
<a href="#1.005">1.005</a><a href="#1.003">1.003</a><a
href="#1.031">1.031</a><a href="#1.017">1.017</a><a
href="#1.016">1.016</a><a href="#29.001">29.001</a><a
href="#30.046">30.046</a><a href="#1.012">1.012</a><a
href="#25.070">25.070</a><a href="#1.011">1.011</a><a
href="#1.012">1.012</a><a href="#1.006">1.006</a><a
href="#1.004">1.004</a><a href="#1.018">1.018</a><a
href="#7.002">7.002</a><a href="#11.098">11.098</a><a
href="#18.011">18.011</a><a href="#18.016">18.016</a><a
href="#25.195">25.195</a><a href="#1.051">1.051</a><a
href="#8.025">8.025</a><a href="#11.059">11.059</a><a
href="#4.012">4.012</a><a href="#1.052">1.052</a><a
href="#1.031">1.031</a><span class="paratext">The
Legal Services Act 2007 has been enacted to establish a new Legal
Services Board as well as an Office for Legal Complaints and also
&ldquo;to make provision for, and in connection with, the regulation
of persons who carry on certain legal activities&rdquo;. A
Commencement No.1 and Transitory Provisions Order has been made (
SI 2008/222) bring...
...reserved legal activities:</span><span class="paratext">
the exercise of a right of audience;
the conduct of litigation;
reserved instrument activities;
probate activities;
notarial activities;
the administration of oaths.</span>

Aaron Patterson

unread,
Nov 25, 2009, 1:22:52 PM11/25/09
to nokogi...@googlegroups.com
Do you think you can put together a stand-alone example that we can
run? I can't really tell what is going on in this example, and I'm
afraid I can't be much help unless I get some code I can actually
execute to reproduce the problem.

Thanks.

--
Aaron Patterson
http://tenderlovemaking.com/
Message has been deleted
Message has been deleted
Message has been deleted

ritchielee

unread,
Nov 25, 2009, 8:20:34 PM11/25/09
to nokogiri-talk
Hi, If it helps, I've sorted out a much simpler gist:

I'm trying with:
xmz = File.open("short.xml", "r")

xm = Nokogiri::XML.parse(xmz)


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

outfile = File.new("short_output.html", "w")
outfile.puts builder.to_html
outfile.close


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
href="#link2location">link2location</a>
<span class="innertext">blah five blah blah </span>
blah six</span></p>
</div>
</body>
</html>

Regards,
Ritchie
Message has been deleted

ritchielee

unread,
Nov 27, 2009, 6:22:07 AM11/27/09
to nokogiri-talk
Hi Aaron
Did I supply enough info for you to see what my
duplication problem is; were you able to run the code?

Here is my current output from this last example - so instead of just
formatting the contents of each paragraph, it removes the xrefs from
where they were originally and duplicates elements across paragraphs.
I'm so sorry about this - all I want it to do is swap from xml to html
tags - just like the structure at the end of my last post.




<!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 blah two blah blah blah
three
<a href="#link1location">link1location</a>
<a href="#link2location">link2location</a>
<span class="innertext"> blah two blah blah </span>
<span class="innertext">blah five blah blah </span>
</span>

<span class="innersection"> blah four blah blah five blah blah blah
six
<a href="#link1location">link1location</a>
<a href="#link2location">link2location</a>
<span class="innertext"> blah two blah blah </span>
<span class="innertext">blah five blah blah </span>
</span>
</p>


<h3 class="para" id="2">2</h3>
<p class="narrativeparagraph">
<span class="innersection"> blah one blah blah two blah blah blah
three
<a href="#link1location">link1location</a>
<a href="#link2location">link2location</a>
<span class="innertext"> blah two blah blah </span>
<span class="innertext">blah five blah blah </span>
</span>

<span class="innersection"> blah four blah blah five blah blah
blah six
<a href="#link1location">link1location</a>
<a href="#link2location">link2location</a>
<span class="innertext"> blah two blah blah </span>
<span class="innertext">blah five blah blah </span>

ritchielee

unread,
Nov 27, 2009, 8:01:04 PM11/27/09
to nokogiri-talk
Any noobs in the same boat as me - I've backed out of trying to use
Builder and I'm getting there slowly but surely with:

frag.xpath("//paragraph").each { |div| div.name= "p";
div.set_attribute("class" , "narrativeparagraph") }

frag.css('inner-section xref').each { |xref| xref.name= "a";
xref.set_attribute("href" , "#" + xref['number']); xref.content = xref
['number'] }
Reply all
Reply to author
Forward
0 new messages