dongmangzong8006 2008-11-29 02:33
浏览 38
已采纳

PHP中的XML解析和转换?

I have a custom XML schema defined for page display that puts elements on the page by evaluating XML elements on the page. This is currently implemented using the preg regex functions, primarily the excellent preg_replace_callback function, eg:

...
$s = preg_replace_callback("!<field>(.*?)</field>!", replace_field, $s);
...

function replace_field($groups) {
  return isset($fields[$group[1]) ? $fields[$groups[1]] : "";
}

Just as an example.

Now this works pretty well... so long as the XML elements aren't nested. At this point it gets a whole lot more complicated, like if you have:

<field name="outer">
  <field name="inner">
    ...
  </field>
</field>

You want to make sure you replace the innermost field first. Judicious use of greedy/non-greedy regex patterns can go some of the way to handling these more complicated scenarios but the clear message is that I'm reaching the limits of what regex can reasonably do and really I need to be doing XML parsing.

What I'd like is an XML transformation package that:

allows me to conditionally evaluate/include the contained document tree or not based on a callback function ideally (analagous to preg_replace_callback); can handle nested elements of the same or different types; and handles attributes in a nice way (eg as an associative array).

What can help me along the way?

  • 写回答

3条回答 默认 最新

  • duanguane1670 2008-11-29 10:19
    关注

    PHP's XSLTProcessor class (ext/xsl - PHP 5 includes the XSL extension by default and can be enabled by adding the argument --with-xsl[=DIR] to your configure line) is quite sophisticated and allows among other things the use of PHP functions within your XSL document by using the XSLTProcessor::registerPHPFunctions() method.

    The following example is shamelessly pinched from the PHP manual page:

    $xml = '<allusers>
     <user>
      <uid>bob</uid>
     </user>
     <user>
      <uid>joe</uid>
     </user>
    </allusers>';
    $xsl = '<?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" 
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:php="http://php.net/xsl">
    <xsl:output method="html" encoding="utf-8" indent="yes"/>
     <xsl:template match="allusers">
      <html><body>
        <h2>Users</h2>
        <table>
        <xsl:for-each select="user">
          <tr><td>
            <xsl:value-of
                 select="php:function(\'ucfirst\',string(uid))"/>
          </td></tr>
        </xsl:for-each>
        </table>
      </body></html>
     </xsl:template>
    </xsl:stylesheet>';
    $xmldoc = DOMDocument::loadXML($xml);
    $xsldoc = DOMDocument::loadXML($xsl);
    
    $proc = new XSLTProcessor();
    $proc->registerPHPFunctions();
    $proc->importStyleSheet($xsldoc);
    echo $proc->transformToXML($xmldoc);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入