Nokogiri: to_s WITHOUT html surrounding's tags?

Hi all

n = Nokogiri::HTML(“

H1

”)
n.to_s

=> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"

"http://www.w3.org/TR/REC-html40/loose.dtd\">\n

H1

Is there a method that only outputs the stuff I’ve read, and not the
whole valid XHTML stuff?

Needed output:

H1

Thanks a lot
Josh

Joshua M. wrote:

Hi all

n = Nokogiri::HTML(“

H1

”)
n.to_s

=> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"

"http://www.w3.org/TR/REC-html40/loose.dtd\">\n

H1

Is there a method that only outputs the stuff I’ve read, and not the
whole valid XHTML stuff?

Needed output:

H1

If all you need is the original input, then why bother running it
through Nokogiri?

Thanks a lot
Josh

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

If all you need is the original input, then why bother running it
through Nokogiri?

Obviously that’s just for presentation purposes… I will apply some
other stuff to the DOM, too…

Joshua M. wrote:

If all you need is the original input, then why bother running it
through Nokogiri?

Obviously that’s just for presentation purposes… I will apply some
other stuff to the DOM, too…

OK. So try again with an example that is closer to what you actually
need.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

OK. So try again with an example that is closer to what you actually
need.

The example above is exactly what I need. I think my question is quite
simple?

On Tue, Oct 13, 2009 at 2:53 PM, Joshua M.
[email protected] wrote:

n = Nokogiri::HTML(“

H1

”)
n.to_s

=> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"

"http://www.w3.org/TR/REC-html40/loose.dtd\">\n

H1

Is there a method that only outputs the stuff I’ve read, and not the
whole valid XHTML stuff?

Well, it’s not XHTML, if you note the doctype :slight_smile: but …

Needed output:

H1

n = Nokogiri::HTML(“

H1

”).xpath(‘//h1’).to_xml
=> “

H1

I would think that’d be pretty apparent from a glance at the examples
in the rdoc, btw. Just sayin’ :slight_smile:


Hassan S. ------------------------ [email protected]
twitter: @hassan

On Tue, Oct 13, 2009 at 2:53 PM, Joshua M. <
[email protected]> wrote:

whole valid XHTML stuff?

Needed output:

H1

Thanks a lot
Josh

You can do the following:

Nokogiri::HTML(“

H1

”).css( ‘h1’ )

Good luck,

-Conrad

Thank you, guys. I didn’t know that Nokogiri creates a complete (X)HTML
DOM when reading an incomplete structure.

Have a great day!

On Tue, Oct 13, 2009 at 4:49 PM, Conrad T. [email protected]
wrote:

">\n

H1

You can do the following:

Nokogiri::HTML(“

H1

”).css( ‘h1’ )

The example above produces a node of the DOM and adding ‘to_s’ will give
you
the string.

Joshua M. wrote:

Thank you, guys. I didn’t know that Nokogiri creates a complete (X)HTML
DOM when reading an incomplete structure.

Have a great day!

Another question. I don’t get Nokogiri to produce XHTML. So for example
I get unclosed tags like


instead of
. I found some options
(http://nokogiri.rubyforge.org/nokogiri/Nokogiri/XML/ParseOptions.html)
but I don’t really know how to get them to work…