Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7521459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:14:17+00:00 2026-05-30T02:14:17+00:00

I would like the node name in the following code to be " node:name

  • 0

I would like the node name in the following code to be "node:name" but instead the name is put into the text of the field.

require 'nokogiri'

file = File.new("/Users/user_a/code/xmler/test.xml", "w+")

builder = Nokogiri::XML::Builder.new do  |xml|
  xml.node:name do
    
  end
end

file << builder.to_xml
file.close
puts builder.to_xml

How can I use the colon or other special characters in a node name with Nokogiri?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:14:18+00:00Added an answer on May 30, 2026 at 2:14 am

    As I see it, you have three options:

    1. You’re using namespaces

      Then you can declare the namespace and use the xml[] method:

      builder = Nokogiri::XML::Builder.new do |xml|
        xml.root('xmlns:node' => 'http://example.com') do
          xml['node'].name
        end
      end
      

      Output:

      <root xmlns:node="http://example.com">
        <node:name/>
      </root>
      

      This method is a bit trickier if you want to add a namespace onto the root element though. See “How to create an XML document with a namespaced root element with Nokogiri Builder“.

    2. You’re not using namespaces but want/need an element name with a colon:

      In this case, you need to send the method named “node:name” to the xml block parameter. You can do this with the normal ruby send method:

      builder = Nokogiri::XML::Builder.new do |xml|
        xml.root do
          xml.send 'node:name'
        end
      end
      

      this outputs:

      <?xml version="1.0"?>
      <root>
        <node:name/>
      </root>
      
    3. You’re not sure what this “namespace” business is all about:

      In this case you’re probably best avoiding using colons in your element names.

      An alternative could be to use - instead. If you did this you’d need to use method 2. above, but with xml.send 'node-name'. I include this option because you don’t mention namespaces in your question, and colons are used in them (as method 1. shows) so you’re safer not using colons to avoid any future problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following xml-node: val xml = <fields><field name=one></field><field name=two></field></fields> Now I would
I would like to convert a string into a node. I have a method
What would it mean to create something like the following? <xsd:simpleType name=myField> <xsd:restriction base=xsd:boolean/>
I would like to know how to implement the result of the following code
I would like to fadeIn a node over one second. Then leave it on
I would like to select a node and modify its attributes and child-nodes using
I have a org.w3c.dom.Node object. I would like to see if it has any
I have a simple TreePanel. I would like to select a particular node upon
I have the following code that converts my twitter account rss feed into a
Hey all, I'm very new to mongodb, mongoose and node.js. I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.