Created
April 14, 2014 14:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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