Last active
December 17, 2015 21:09
-
-
Save felds/5672335 to your computer and use it in GitHub Desktop.
Exemplo de substituição de node aplicado em um AIML.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <aiml version="1.0"> | |
| <template> | |
| Começo do texto ”<srai>include</srai>” fim do texto. | |
| </template> | |
| </aiml> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env php | |
| <?php | |
| // carrega o XML | |
| $domdoc = new DomDocument; | |
| $domdoc->preserveWhitespace = true; | |
| $domdoc->load('example.aiml'); | |
| // cria um navegador xpath | |
| $xpath = new DomXPath($domdoc); | |
| // pega os nodes que a gente quer | |
| foreach ($xpath->query('//template') as $template) { | |
| // pega os nodes <srai> dentro dos templates | |
| foreach ($xpath->query('srai', $template) as $srai) { | |
| // // pega o que tem dentro do node <srai> | |
| // $value = $srai->nodeValue; | |
| // cria o novo textnode | |
| $newContent = $domdoc->createTextNode("meio do texto"); | |
| // substitui o node <srai> pelo textnode | |
| $template->replaceChild($newContent, $srai); | |
| } | |
| } | |
| // output | |
| $domdoc->formatOutput = true; | |
| echo $domdoc->saveXML(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment