As nesting XML data is a common need, I was really wondering how many users need to solve this problem.
Here is a quick and clean solution:
Wanted Output:
<item>
<items>
<case>
<szenario:1>My Szenario 1</szenario:1>
<szenario:2 ClassificationSchema="A">My Szenario 2</szenario:2>
</case>
</items>
</item>
The wrapping of <item> and <items> is already done by the views data export modul. If you want to change the text, just jump into the views format options.
The trouble starts here: how to nest <szenario:1> and <szenario:2> into <case>?
Adding some fields
In our example we add the fields
- szenario:1 (= field_szenario1)
- szenario:2 (= field_szenario2)
Views PHP
Install Views PHP: https://www.drupal.org/project/views_php
In Views add the field global:PHP.
Name it: case.
In the "output code" field you can nest the fields.
Watch out! You need to call the values for the fields.
This can be done by using a snippet like this: print $data->field_field_szenario1[0]['raw']['value']
Don't miss to write 2 times field!
A propper PHP code for our example will look like this:
<?php
echo '<szenario:1>'; print $data->field_field_szenario1[0]['raw']['value']; echo '</szenario:1>';
echo '<szenario:2 ClassificationSchema="A">'; print $data->field_field_szenario2[0]['raw']['value']; echo '</szenario:2>';
?>Clean display in XML
To be sure, that < and > are printed in the correct way, finally jump again into the views format options.
You will find there the option "Disable encoding of XML entities for these fields".
Select the global PHP field.
Enjoy :)
Comments
Comment #2
CarolaZ commented