A SPARQL Query Result Parser written in PHP
Inspired by the SPARQL Query Result Parser written by Daniel Alexander Smith and published on the Enakting Blog, I just wrote an implementation in PHP. It utilizes the performant event based XML parser of PHP. In contrast to the original implementation written in Python, the current version below supports also the “datatype” and “xml:lang” attribute of literal types.
I have stripped off most of the comments and the copyright notice for better readability. You will find the full code on the project page. It also comes with unit tests.
This is just a small part of our attempt to integrate some techologies of the Semantic Web Stack into TYPO3. For the current status of the project and our future plans, please see our product backlog.
The script is part of the Semantic Web Integration project of TYPO3. You might also want to have a look at our roadmap or visit the Homepage of the TYPO3 community.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | <?php class Tx_Semantic_Domain_Model_Sparql_QueryResultParser implements Tx_Semantic_Domain_Model_Sparql_QueryResultParserInterface { protected $parser; protected $results = array(); protected $currentResult = array(); protected $currentName = ''; protected $currentCharacterData = ''; protected $currentType = ''; protected $currentDataType; protected $currentLanguage; protected $processCharacterData = FALSE; public function __construct() { $this->parser = xml_parser_create(); xml_set_object($this->parser, $this); xml_set_element_handler($this->parser, 'handleElementStart', 'handleElementStop'); xml_set_character_data_handler($this->parser, 'handleCharacterData'); } public function __destruct() { xml_parser_free($this->parser); } public function parse($document) { $status = xml_parse($this->parser, $document); if ($status === 1) { return $this->results; } else { throw new Tx_Semantic_Domain_Model_Sparql_Exception_QueryResultParserException('Parser Error: "' . xml_error_string(xml_get_error_code($this->parser)) . '".', 1296481762); } } protected function handleElementStart($parser, $elementName, $elementAttributes) { switch ($elementName) { case 'VARIABLE': $this->results['variables'][] = $elementAttributes['NAME']; $this->processCharacterData = FALSE; break; case 'BINDING': $this->currentName = $elementAttributes['NAME']; $this->processCharacterData = FALSE; break; case 'LITERAL': $this->currentType = 'literal'; if (isset($elementAttributes['DATATYPE'])) { $this->currentDatatype = $elementAttributes['DATATYPE']; } if (isset($elementAttributes['XML:LANG'])) { $this->currentLanguage = $elementAttributes['XML:LANG']; } $this->processCharacterData = TRUE; break; case 'BNODE': $this->currentType = 'bnode'; $this->processCharacterData = TRUE; break; case 'URI': $this->currentType = 'uri'; $this->processCharacterData = TRUE; break; } } protected function handleElementStop($parser, $elementName) { switch ($elementName) { case 'BINDING': $this->currentResult[$this->currentName] = array( 'name' => $this->currentName, 'value' => $this->currentCharacterData, 'type' => $this->currentType ); if ($this->currentDatatype !== NULL) { $this->currentResult[$this->currentName]['datatype'] = $this->currentDatatype; $this->currentDatatype = NULL; } if ($this->currentLanguage !== NULL) { $this->currentResult[$this->currentName]['language'] = $this->currentLanguage; $this->currentLanguage = NULL; } $this->currentCharacterData = ''; break; case 'LITERAL': $this->processCharacterData = FALSE; break; case 'BNODE': $this->processCharacterData = FALSE; break; case 'URI': $this->processCharacterData = FALSE; break; case 'RESULT': $this->results['results'][] = $this->currentResult; $this->currentResult = array(); break; } } protected function handleCharacterData($parser, $characterData) { if ($this->processCharacterData === TRUE) { $this->currentCharacterData .= $characterData; } } } ?> |
+1 for using xml_parser instead of simplexml ;)