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

Skip to first unread message

Wayne Brissette

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?


Mike Dalessio

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 = do |xml|
  xml.root {
    xml.products {
      xml.send("referable-content") {
        xml.id_ "10" "Awesome widget"
puts builder.to_xml


<?xml version="1.0"?>
      <name>Awesome widget</name>

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
To view this discussion on the web visit

Wayne Brissette

Jul 1, 2020, 1:50:40 PM7/1/20
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 Brissette

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


Reply all
Reply to author
0 new messages