{"id":99,"date":"2010-01-29T12:12:15","date_gmt":"2010-01-29T18:12:15","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=99"},"modified":"2015-10-28T10:05:47","modified_gmt":"2015-10-28T15:05:47","slug":"mysqls-xml-and-some-xslt","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=99","title":{"rendered":"mysql&#8217;s <code>--xml<\/code> and some XSLT (now with JSON!)"},"content":{"rendered":"<p>Somebody asked in Freenode the other day how to get their data out of MySQL into a specific XML format.<\/p>\n<p>Both mysqldump and the mysql client have <code>--xml<\/code> options. The output from the cli looks like this:<\/p>\n<p><code>&lt;resultset statement=\"SELECT here\"&gt;<br \/>\n&nbsp;&nbsp;&lt;row&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;field name=\"field1\"&gt;value a&lt;\/field&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;field name=\"field2\"&gt;value b&lt;\/field&gt;<br \/>\n&nbsp;&nbsp;&lt;\/row&gt;<br \/>\n&nbsp;&nbsp;&lt;row&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;field name=\"field1\"&gt;value c&lt;\/field&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;field name=\"field2\"&gt;value d&lt;\/field&gt;<br \/>\n&nbsp;&nbsp;&lt;\/row&gt;<br \/>\n&lt;\/resultset&gt;<\/code><\/p>\n<p>I wanted to change those &lt;field name=&#8221;fieldName&#8221;&gt; into &lt;fieldName&gt; tags, and all it takes is a little XSLT, which I&#8217;ve uploaded <a href=\"http:\/\/www.thenoyes.com\/storage\/mysql.xsl\">here<\/a>.<\/p>\n<p>An XSLT processor lets you do something like this:<\/p>\n<p><code>mysql --xml -e \"SELECT here\" | xsltproc mysql.xsl -<\/code><\/p>\n<p>The result will be something like:<\/p>\n<p><code>&lt;resultset statement=\"SELECT here\"&gt;<br \/>\n&nbsp;&nbsp;&lt;row&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;field1&gt;value a&lt;\/field1&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;field2&gt;value b&lt;\/field2&gt;<br \/>\n&nbsp;&nbsp;&lt;\/row&gt;<br \/>\n&nbsp;&nbsp;&lt;row&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;field1&gt;value c&lt;\/field1&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;field2&gt;value d&lt;\/field2&gt;<br \/>\n&nbsp;&nbsp;&lt;\/row&gt;<br \/>\n&lt;\/resultset&gt;<\/code><\/p>\n<p>If JSON is your thing, you can use <a href=\"https:\/\/github.com\/bojanbjelic\/xml2json\/\">xml2json<\/a> to take that output a step farther:<\/p>\n<p><code>mysql --xml -e \"SELECT here\" | xsltproc mysql.xsl - | xsltproc xml2json.xsl -<\/code><\/p>\n<p>Result (whitespace added):<\/p>\n<pre>{\"resultset\" : \r\n  {\r\n    \"statement\" : \"SELECT here\", \r\n    \"row\" : [\r\n      {\"field1\" : \"value a\", \"field2\" : \"value b\"},\r\n      {\"field1\" : \"value c\", \"field2\" : \"value d\"}\r\n    ]\r\n  }\r\n}<\/pre>\n<p>That will suffice until the mysql client has a &#8211;json parameter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Somebody asked in Freenode the other day how to get their data out of MySQL into a specific XML format. Both mysqldump and the mysql client have &#8211;xml options. The output from the cli looks like this: &lt;resultset statement=&#8221;SELECT here&#8221;&gt; &nbsp;&nbsp;&lt;row&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;field name=&#8221;field1&#8243;&gt;value a&lt;\/field&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;field name=&#8221;field2&#8243;&gt;value b&lt;\/field&gt; &nbsp;&nbsp;&lt;\/row&gt; &nbsp;&nbsp;&lt;row&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;field name=&#8221;field1&#8243;&gt;value c&lt;\/field&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;field name=&#8221;field2&#8243;&gt;value d&lt;\/field&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[8],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-mysql-faq"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-1B","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=99"}],"version-history":[{"count":11,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":431,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions\/431"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}