Self-closing tags inconstancy

348 views
Skip to first unread message

Ngan

unread,
Aug 2, 2013, 3:08:26 PM8/2/13
to nokogi...@googlegroups.com
Hello,

On my computer (libxml2 installed), I get the following:

>> Nokogiri::XML::Builder.new {|b| b.first_name "" }.to_xml
=> "<?xml version=\"1.0\"?>\n<first_name/>\n"

On my coworkers' computer (libxml2 not installed), they all get the following:

>> Nokogiri::XML::Builder.new {|b| b.first_name "" }.to_xml
=> "<?xml version=\"1.0\"?>\n<first_name></first_name>\n"

I know they are technically "the same" according to specifications...but does anyone know the reason why this is happening? Is is all dependent on the underlining xml library that Nokogiri was compiled against?  Also, is there a way to force tag expansion?  The API that I'm hitting does not recognize self-closing tags, unfortunately.

Thanks!

Ben Langfeld

unread,
Aug 2, 2013, 3:57:12 PM8/2/13
to nokogi...@googlegroups.com
This is indeed something that libxml2 is responsible for, and may well be different depending on which version you compile against. Nokogiri does not to my knowledge have an API for forcing this expansion precisely because the XML spec is indifferent to it.

You could try including a space to fool libxml into thinking there is content, though this is also called being evil...


--
You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nokogiri-tal...@googlegroups.com.
To post to this group, send email to nokogi...@googlegroups.com.
Visit this group at http://groups.google.com/group/nokogiri-talk.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jonathan Rochkind

unread,
Aug 2, 2013, 4:05:33 PM8/2/13
to nokogi...@googlegroups.com
Another idea -- that I would still consider an unreliable hack, that I'm
not sure I'd count on -- is pretending it's HTML instead of XML. As HTML
(depending on the version/variation) does not allow self-closing tags,
Nokogiri/LibXML's HTML tools generally avoid them:

Nokogiri::HTML::Builder.new {|b| b.first_name "" }.to_html
=> "<first_name></first_name>"

But I'm not sure I'd actually do that, or rely upon it, myself. It still
seems at risk of breaking in different versions of nokogiri -- or if
your XML includes features that actually are incompatible with
pretending it's HTML (not sure what those would be, but I bet there are
some).

In general, while nokogiri is quite good (and has little mature
competion in ruby) at parsing and analyzing already existing XML, it's
XML production facilities are somewhat less polished, and it's got more
competition there. You might look into alternate solutions for producing
XML, and see if any of them meet your needs of offering documented and
explicit control over how empty tags are serialized.

On 8/2/13 3:57 PM, Ben Langfeld wrote:
> This is indeed something that libxml2 is responsible for, and may well
> be different depending on which version you compile against. Nokogiri
> does not to my knowledge have an API for forcing this expansion
> precisely because the XML spec is indifferent to it.
>
> You could try including a space to fool libxml into thinking there is
> content, though this is also called being evil...
>
>
> On 2 August 2013 16:08, Ngan <ngan...@gmail.com
> <mailto:ngan...@gmail.com>> wrote:
>
> Hello,
>
> On my computer (libxml2 installed), I get the following:
>
> >> Nokogiri::XML::Builder.new {|b| b.first_name "" }.to_xml
> => "<?xml version=\"1.0\"?>\n<first_name/>\n"
>
> On my coworkers' computer (libxml2 not installed), they all get the
> following:
>
> >> Nokogiri::XML::Builder.new {|b| b.first_name "" }.to_xml
> => "<?xml version=\"1.0\"?>\n<first_name></first_name>\n"
>
> I know they are technically "the same" according to
> specifications...but does anyone know the reason why this is
> happening? Is is all dependent on the underlining xml library that
> Nokogiri was compiled against? Also, is there a way to force tag
> expansion? The API that I'm hitting does not recognize self-closing
> tags, unfortunately.
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google
> Groups "nokogiri-talk" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to nokogiri-tal...@googlegroups.com
> <mailto:nokogiri-talk%2Bunsu...@googlegroups.com>.
> To post to this group, send email to nokogi...@googlegroups.com
> <mailto:nokogi...@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages