Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.1.4dev1

Request #8007 Options for customizing XML renderer
Submitted: 2006-06-26 16:58 UTC
From: derernst Assigned: olivierg
Status: Closed Package: Structures_DataGrid_Renderer_XML (version 0.1.0)
PHP Version: 4.3.9 OS:
Roadmaps: (Not assigned)    
Comments Add Comment Add patch

Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem : 36 - 7 = ?

 [2006-06-26 16:58 UTC] derernst (Markus Ernst)
Description: ------------ Here is my suggestion for introducing some new options in the XML renderer, allowing to customize the xml outputted: 1. Choose the property used for the item tag name - field property of column (default, as in current version) - label property of column - index counted from 0 to n 2. Define prefix for item tag names (to make numeric values from field property or index wellformed) 3. Possibility to add column field or label properties as attributes to item tag The code posted in the Test script section works for me; I hope it will be considered as useful... Test script: --------------- /** * Changes in Structures_DataGrid_Renderer_XML */ // Changed constructor: added 3 options function Structures_DataGrid_Renderer_XML() { parent::Structures_DataGrid_Renderer(); $this->_addDefaultOptions( array( 'useXMLDecl' => true, 'outerTag' => 'DataGrid', 'rowTag' => 'Row', 'itemTagProperty' => 'field', // possible values: 'field', 'label', 'index' 'itemTagPrefix' => '', // allows making numeric item tags wellformed by adding a prefix 'itemTagAttributes' => array() // format i.e.: array('id' => 'field', 'name' => 'label') ) ); } // Changed row building method function buildRow($index, $data) { $this->_xml .= " <{$this->_options['rowTag']}>\n"; foreach ($data as $col => $value) { $f = $this->_options['itemTagProperty'] == 'index' ? $i++ : $this->_columns[$col][$this->_options['itemTagProperty']]; $field = $this->_options['itemTagPrefix'].$f; $a = array(); foreach ($this->_options['itemTagAttributes'] as $k => $v) { $a[$k] = $this->_columns[$col][$v]; } $attributes = empty($a) ? null : $a; $this->_xml .= ' ' . XML_Util::createTag($field, $attributes, $value) . "\n"; } $this->_xml .= " </{$this->_options['rowTag']}>\n"; } /** * Use in script */ $fields = array(2 => 'Last name', 1 => 'First name', 'lang' => 'Language'); $data = array( array(2 => 'Smith', 1 => 'John', 'lang' => 'English'), array(2 => 'Schmid', 1 => 'Hans', 'lang' => 'German'), array(2 => 'Dupont', 1 => 'Jean', 'lang' => 'French') ); // Get datagrid object, bind data require_once 'Structures/DataGrid.php'; $datagrid =& new Structures_DataGrid(); $datagrid->bind($data); // Set columns foreach ($fields as $field_id => $field_name) { $datagrid->addColumn(new Structures_DataGrid_Column($field_name, $field_id)); } // Output XML $datagrid->setRenderer(DATAGRID_RENDER_XML); $renderer =& $datagrid->getRenderer(); $renderer->setOptions(array('outerTag' => 'UserList', 'rowTag' => 'User')); // First case: //Default (same behaviour as current version) $datagrid->render(); exit; // Second case: // Use Column label instead of field property // (Would need tag names without spaces to be wellformed!) $renderer->setOptions(array('itemTagProperty' => 'label')); $datagrid->render(); exit; // // Third case: total customization $xml_options = array( 'itemTagProperty' => 'index', 'itemTagPrefix' => 'item', 'itemTagAttributes' => array('id' => 'field', 'name' => 'label') ); $renderer->setOptions($xml_options); $datagrid->render(); exit; Expected result: ---------------- First case output (as in current version): <?xml version="1.0"?> <UserList> <User> <2>Smith</2> <1>John</1> <lang>English</lang> </User> <User> <2>Schmid</2> <1>Hans</1> <lang>German</lang> </User> <User> <2>Dupont</2> <1>Jean</1> <lang>French</lang> </User> </UserList> Second case output <?xml version="1.0"?> <UserList> <User> <Last name>Smith</Last name> <First name>John</First name> <Language>English</Language> </User> <User> <Last name>Schmid</Last name> <First name>Hans</First name> <Language>German</Language> </User> <User> <Last name>Dupont</Last name> <First name>Jean</First name> <Language>French</Language> </User> </UserList> Third case output <?xml version="1.0"?> <UserList> <User> <item0 id="2" name="Last name">Smith</item0> <item1 id="1" name="First name">John</item1> <item2 id="lang" name="Language">English</item2> </User> <User> <item0 id="2" name="Last name">Schmid</item0> <item1 id="1" name="First name">Hans</item1> <item2 id="lang" name="Language">German</item2> </User> <User> <item0 id="2" name="Last name">Dupont</item0> <item1 id="1" name="First name">Jean</item1> <item2 id="lang" name="Language">French</item2> </User> </UserList>


 [2006-06-26 17:05 UTC] olivierg at php dot net (Olivier Guilyardi)
Looks great :-) I'm gonna try to integrate this. But please provide a patch next time. See:
 [2006-06-26 17:20 UTC] olivierg at php dot net (Olivier Guilyardi)
The "label" style (second case) does not make sense to me. First it's gonna lead to very badly formed XML : you can't have spaces into a tag name. And anyway, the tag name is an identifier, where the $columnName (== label) is for presentation purpose (and is likely to contain spaces...). The "index" style (third case) is useless. It's not coherent with XML. You'll never be able to make a DTD/Schema out of that. The following is more appropriate : <item id="2" name="Last name">Smith</item> <item id="1" name="First name">John</item> <item id="lang" name="Language">English</item> Why do you want to append a numbered suffix to the tag name ?
 [2006-06-26 18:15 UTC] derernst
> Why do you want to append a numbered suffix to the tag name ? Because my XML knowledge is really poor... ;-) Thank you for pointing me to the contributing page, too - a link to a newbie-proof introduction to CVS would actually be helpful there; I have no idea about CVS, and none about shells etc either, which is a major barrier for PHP-only coders like me without basic IT education.
 [2006-06-26 19:58 UTC] wiesemann (Mark Wiesemann)
The second case is really not usable, as it would generate XML that is not well-formed. The third one looks okay to me (with the format from Olivier, i.e. without the numbering on the tag name). (I've corrected the package selection and the package version.)
 [2006-06-26 20:41 UTC] olivierg at php dot net (Olivier Guilyardi)
Okay, I've added the following options : - fieldTag: The name of the tag for each field inside a row, without brackets. The special value '{field}' is replaced by the field name. (default: '{field}') - fieldAttribute: The name of the attribute for the field name. null stands for no attribute (default: null) - labelAttribute: The name of the attribute for the column label. null stands for no attribute (default: null) So for the third case you mentionned, without numbering, you need : $options = array( 'fieldTag' => 'item', 'fieldAttribute' => 'id', 'labelAttribute' => 'name'); $datagrid->render(DATAGRID_RENDER_XML, $options); What do you both think about this ?
 [2006-06-26 20:48 UTC] wiesemann (Mark Wiesemann)
Didn't try it, but the explaination here and the code look good.
 [2006-06-27 15:21 UTC] derernst
I tried it and like it!
 [2006-06-27 18:50 UTC] olivierg at php dot net (Olivier Guilyardi)
Fine. This bug is closed. Thanks for your report