How do I build an XML type with a dash(hyphen) in it?

9 views
Skip to first unread message

Wayne Brissette

unread,
Jul 1, 2020, 1:21:17 PM7/1/20
to nokogiri-talk
I'm running into an issue where our CMS vendor has specialized one of
the XML doc types. It is <referable-content>

In builder when I try to use the dash it thinks I'm trying to perform a
math operation. I tried quoting it, but that didn't work either... Is
there a way to do this?

-Wayne

Mike Dalessio

unread,
Jul 1, 2020, 1:42:16 PM7/1/20
to nokogiri-talk
Not totally sure if this is what you're asking, but:

#! /usr/bin/env ruby

require "nokogiri"

builder = Nokogiri::XML::Builder.new do |xml|
  xml.root {
    xml.products {
      xml.send("referable-content") {
        xml.id_ "10"
        xml.name "Awesome widget"
      }
    }
  }
end
puts builder.to_xml

generates:

<?xml version="1.0"?>
<root>
  <products>
    <referable-content>
      <id>10</id>
      <name>Awesome widget</name>
    </referable-content>
  </products>
</root>

Tag names are simply a message being sent to the builder object, which intercepts with #method_missing. Does that make sense?


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/nokogiri-talk/46db8a99-ad73-5c94-ce98-811622e2893a%40att.net.

Wayne Brissette

unread,
Jul 1, 2020, 1:50:40 PM7/1/20
to nokogi...@googlegroups.com
Ah... just found an answer on stackoverflow that Aaron posted on how to
do it. A bit messy since this is the parent element for 98% of the
content, but I'll make it work.

-Wayne

Wayne Brissette

unread,
Jul 1, 2020, 2:30:57 PM7/1/20
to nokogi...@googlegroups.com
Thanks Mike. That's exactly what I was after. I'm still not sure why our
CMS vendor used a hyphen.

-Wayne

Reply all
Reply to author
Forward
0 new messages