Zendu Form Object to XML数据表示(用于XSLT)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zendu Form Object to XML数据表示(用于XSLT)相关的知识,希望对你有一定的参考价值。
Turns a Zend_Form object into XML data. Click the associated link to see an Example XML result and a XSL Stylesheet used to display regular HMTL.
class XML_Form extends XML_Helper { /** * * Turns a Zend_Form object into a DOMDocument for use in your XSLT-powered frontend * @param Zend_Form $object * @param array $data to validate form against * @return DOMDocument */ public static function formToDOM(Zend_Form $object=null, $data=null) { if ($object->isValid($data)) $state = "valid"; else $state = "fail"; } else { $state = "init"; } $doc = new DOMDocument('1.0', 'UTF-8'); $doc->loadXML('<item type="form" />'); $root = $doc->documentElement; // 1st level item $item = $doc->getElementsByTagName('item')->item(0); $item->setAttribute('id',$object->getAttrib('id')); $item->setAttribute('state',$state); // 2nd level fragment form properties $options = $object->getAttribs(); $options['enctype'] = $object->getEnctype(); foreach ($options as $attribute => $value) { $fragment = $doc->createElement('fragment',$value); $fragment->setAttribute('id',$attribute); $item->appendChild($fragment); } // 3rd level group for form elements $group_fields = $doc->createElement('group'); $group_fields->setAttribute('id','fields'); foreach ($object->getElements() as $element) { /** set redirect value if <input type="hidden" name="redirect" /> is present in current form and $_POST * @todo test redirect functionality, retrieve redirect value in **/ if ($class === 'hidden' && $element->getName() === 'redirect' && isset($_SESSION['reservationform'])) { $lang = Royal_Language::getLanguage(); $region = Royal_FrontendRegion::getRegion(); $element->setValue("/$lang/$region/memberhome/"); } // 3rd level form elements $item_element = $doc->createElement('item'); $item_element->setAttribute('id',$element->getName()); $item_element->setAttribute('type', $class); //4th level fragments of element parent::createFragment('name', $item_element, $element->getName()); parent::createFragment('label', $item_element, $element->getLabel()); parent::createFragment('value', $item_element, $element->getValue()); parent::createFragment('required', $item_element, $element->isRequired()); parent::createFragment('maxlength', $item_element, $element->getAttrib('maxlength')); parent::createFragment('size', $item_element, $element->getAttrib('size')); parent::createFragment('readonly', $item_element, $element->getAttrib('readonly')); parent::createFragment('description', $item_element, $element->getDescription()); foreach ($element->getErrors() as $error) { parent::createFragment('error', $item_element, $error); } } // possible errors if ($element->isValid($data[$element->getName()])===true) { $valid = 'true'; } else { $valid = 'false'; } parent::createFragment('valid', $item_element, $valid); $group_options = parent::createGroup('options', $item_element); foreach ($element->getMultiOptions() as $key => $text) { $item_option = $doc->createElement('item'); $item_option->setAttribute('id', 'option'); parent::createFragment('text', $item_option, $text); parent::createFragment('value', $item_option, $key); $group_options->appendChild($item_option); } } $group_fields->appendChild($item_element); $item->appendChild($group_fields); } return $doc->saveXML($root, LIBXML_NOEMPTYTAG); // finally return DOMDocument } else { throw new Exception("Parameter <samp>$object</samp> passed to XML_Form::formToDOM() is required and needs to be of type Zend_Form"); } } } class XML_Helper { /** * * __constructor */ public function Royal_XML_Creator() { } /** * * Appends a <fragment id='id'>value</fragment> to a given origin and returns the node for further use * @param string $id * @param DOMNode $origin * @param string $value * @return DOMNode */ public static function createFragment($id, DOMNode $appendTo, $value='') { /** @todo maybe encode the value in order to make sure the XML stays valid (non-UTF-8 characters, html, etc.) **/ $fragment = $appendTo->ownerDocument->createElement('fragment', $value); $fragment->setAttribute('id', (string) $id); $appendTo->appendChild($fragment); return $fragment; } /** * * Appends a <fragment id='id'><![CDATA[value]]></fragment> to a given DOM * @param unknown_type $id * @param DOMNode $origin * @param unknown_type $value */ public static function createCDATAFragment($id, DOMNode $appendTo, $value='') { $fragment = $appendTo->ownerDocument->createElement('fragment'); $fragment->appendChild($appendTo->ownerDocument->createCDATASection($value)); $fragment->setAttribute('id', $id); $appendTo->appendChild($fragment); return $fragment; } /** * * Appends a group <group id='id' /> to a given origin * @param string $id * @param DOMNode $origin * @return DOMNode */ public static function createGroup($id, DOMNode $appendTo) { $group = $appendTo->ownerDocument->createElement('group'); $group->setAttribute('id', $id); $appendTo->appendChild($group); return $group; } } /** Example Usage: **/ $proc = new XSLTProcessor(); $xsldoc = new DOMDocument(); $xsldoc->load(APPLICATION_PATH.'/xsl/forms.xsl'); $proc->importStyleSheet($xsldoc); echo $proc->transformToXML(new XML_Form::formToDOM(new My_Custom_Zend_Form()));
以上是关于Zendu Form Object to XML数据表示(用于XSLT)的主要内容,如果未能解决你的问题,请参考以下文章
C# Object to Json to Xml,如何为数组项生成xml元素
Oracle Form (FMB) to XML,啥是dirtyinfo