Skip to content

Instantly share code, notes, and snippets.

@mallorydxw
Created April 14, 2014 14:40
Show Gist options
  • Save mallorydxw/10654187 to your computer and use it in GitHub Desktop.
Save mallorydxw/10654187 to your computer and use it in GitHub Desktop.
The wordpress-importer plugin doesn't understand namespaces. Here's a workaround that converts XML generated by Go into XML this plugin understands.
#!/bin/ruby
# Read an XML document on STDIN, write an XML document on STDOUT
#
# Replace
# <wxr_version xmlns="http://wordpress.org/export/1.2/">1.2</wxr_version>
# with
# <wp:wxr_version>1.2</wp:wxr_version>
# and add xmlns:wp=... to the root node
require 'nokogiri'
namespaces = {
'excerpt' => 'http://wordpress.org/export/1.2/excerpt/',
'content' => 'http://purl.org/rss/1.0/modules/content/',
'wfw' => 'http://wellformedweb.org/CommentAPI/',
'dc' => 'http://purl.org/dc/elements/1.1/',
'wp' => 'http://wordpress.org/export/1.2/',
}
doc = Nokogiri::XML(STDIN)
namespaces.each do |k,v|
doc.root.add_namespace_definition(k, v)
end
doc.xpath('//*').each do |x|
namespaces.each do |k,v|
if x.namespace && x.namespace.href == v
x.name = k+':'+x.name
end
end
end
puts doc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment