I wanted to expose the result of a views as only a fragment of an XML. I did this by adding a display plugin of my own that didn't wrap the output of Views XML in HTML and by selecting "Views API mode" - but I also needed to be able to remove the doctype and some of the wrapping tags.

Attached is a patch that adds an option to remove the doctype as well as hides the root element and top-level child element if they are empty.

With Views XML patched this way you can do some pretty insanse stuff with it and Views Field View

(I would btw suggest that you add some kind of display plugin that can Views XML can be paired with - either a super easy one that just removes all wrapping HTML or one that adds parts of the stuff that's now in the Views XML style plugin.)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

patcon’s picture

I always seem to catch you around, Pelle...! Overlapping interests I suppose. Awesome share, by the way. I'll let you know if I build anything cool with it :)

jvanhoose’s picture

I am working on a project which would be greatly benefited by using the "view in view" and this xml fragment patch (I need to dynamically generate a somewhat complex XML document). I installed the patch, and it seemed to work just fine the first time (the menu option showed up to enable "xml fragment" - and it worked properly). The next time, however, it not only didn't work - but the menu option appears to have disappeared.

I am certain that this is just something I am doing stupidly - but, any insight would be greatly appreciated.

patcon’s picture

Sorry if this is stupid suggestion, but if it was working and then suddently wasn't, then perhaps it's a site cache issue? Try clearing

obrienmd’s picture

sub

gMaximus’s picture

Hi,

Thanks for sharing this awesome patch. I'm currently trying to build a video sitemap of my "media: youtube" embeds using your patch and other suggestions. However, as you say i need:

some kind of display plugin that can Views XML can be paired with - either a super easy one that just removes all wrapping HTML

I was really hoping that you may be able to point me in the right direction... :) Without out that, my parent view with the page display "video-sitemap.xml" doesn't come out as an xml file...

This patch was my first, so i feel like I've climbed a mountain to get this close... lol

Would really appreciate any help,

Guy

voxpelli’s picture

@guyd: I apparently never pushed it to Drupal.org, but here is what I remember that I used: https://github.com/voxpelli/drupal-plain-views Feel free to fork and commit to Drupal.org or whatever you like - I'm likely not going to do any more work on it myself :/

gMaximus’s picture

Thanks a million for the lead. It was my birthday yesterday, so great timing :).. I'll investigate it fully in the week.... Here's the post i've made fyi:

http://drupal.org/node/1472776

Bye for now,
Guy

gMaximus’s picture

Just finished making my video sitemap, thanks in large part to you... Just wanted to say thanks again :)

cornelyus’s picture

Would this work for Drupal 7?

Thank you

iamfredrik’s picture

I made a patch for D7. The patch seems to work, but I haven't figured out how to get it to play nicely with Views field view. Views field view seems to strip all xml tags.

andypost’s picture

Looks module have no tests, then they are not needed

+++ b/views/plugins/views_plugin_style_xml.inc
@@ -53,6 +53,10 @@ class views_plugin_style_xml extends views_plugin_style {
+	$options['is_xml_fragment'] = array(
+		'default' => FALSE, ¶
+		'translatable' => FALSE
+	);

@@ -151,6 +155,12 @@ class views_plugin_style_xml extends views_plugin_style {
+	$form['is_xml_fragment'] = array(
...
+	  '#title'          => ('Is XML fragment'),
+	  '#default_value'  => $this->options['is_xml_fragment'],
+	  '#description'    => ('If on then no doctype will be printed.'),
+	);

+++ b/views/theme/views_views_xml_style.theme.inc
@@ -24,13 +24,15 @@ function template_preprocess_views_views_xml_style_raw(&$vars) {
+	  if (!empty($top_child_object)) {
+	  		  $xml .= ($options['element_output'] == 'nested') ? "  <$top_child_object>\n": "  <$top_child_object\n";
+	  }

@@ -89,9 +91,11 @@ function template_preprocess_views_views_xml_style_raw(&$vars) {
+	if (!empty($end_top_child_object)) {
+	    $xml .= ($options['element_output'] == 'nested') ? "  </$end_top_child_object>\n": "/>\n";
+	}

please replace tabs with spaces

andypost’s picture

Version: 6.x-1.0-beta2 » 7.x-1.x-dev

Also the code sill works for 7.x

heshanlk’s picture

Rerolled the patch and here are few tips to make it work with Views Field View module.

Tips:

  • Enable "Views API mode" if you enabled "Is XML fragment" checkbox. So all fragments should use the Views API mode.
  • Remove unnecessary HTML from Display output tpl file and place it in template directory of your theme.
  • Go to parent display (which has the one with "Is XML fragment" disabled) and uncheck "Plaintext output".
kenorb’s picture

Re-roll of #13 with 2 spaces instead of tabs and removed trailing spaces. And the right format for git repo.