简单的模板 XSLT

2.2.0 2018-10-17 08:56 UTC

README

Build Status

  • 扩展自 PHP 内置的 \XSLTProcessor 类。
    • 支持所有 \XSLTProcessor 功能
  • 支持 PSR-11 容器
  • 一些新的小语法

PSR-11 容器支持

<!-- new xslt function syntax -->
this:container(...)

this:container('CONTAINER_SCALAR_ITEM_NAME')

this:container('CONTAINER_FUNCTION_ITEM_NAME', 'PARAMETER_1', 'PARAMETER_2', '...')

this:container('CONTAINER_OBJECT_ITEM_NAME', 'METHOD_NAME', 'FIRST_PARAMETER', 'SECOND_PARAMETER', '...')

使用示例

<xsl:value-of select="this:container('scalar_item_name')/>
<xsl:value-of select="this:container('function_item_name', 'foobar', 123)/>
<xsl:value-of select="this:container('object_item_name', 'method_name', 'arg_1', 'arg2')/>

此示例代码仅适用于 \Stex\StexXsltProcessor 类!

$xml = <<<EOB
<allusers>
 <user>
  <uid>bob</uid>
 </user>
 <user>
  <uid>joe</uid>
 </user>
</allusers>
EOB;
$xsl = <<<EOB
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:php="https://php.ac.cn/xsl">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
 <xsl:template match="allusers">
  <html><body>
    <h2>Users</h2>
    <table border="1" cellspacing="0" cellpadding="5">
      <tr>
        <th>
            Name <br />(php:function)
        </th>
        <th>
            foobar text <br />(this:container)
        </th>
        <th>
            formatted datetime <br />(this:container)
        </th>
      </tr>
    <xsl:for-each select="user">
      <tr>
        <td>
            <xsl:value-of
                select="php:function('ucfirst',string(uid))"/>
        </td>
        <td>
            <xsl:value-of
                select="this:container('foo')"/>
        </td>
        <td>
            <xsl:value-of
                select="this:container('datetime', 'format', 'Y-m-d')"/>
        </td>
      </tr>
    </xsl:for-each>
    </table>
  </body></html>
 </xsl:template>
</xsl:stylesheet>
EOB;
$xmldoc = new \DOMDocument();
$xmldoc->loadXML($xml);
$xsldoc = new \DOMDocument();
$xsldoc->loadXML($xsl);

// define container and container items
$container = new Container();
$container->add('foo', 'bar');
$container->add('datetime', function(){
    return new \DateTime();
});

$proc = new StexXsltProcessor();
$proc->setContainer($container);
$proc->registerPHPFunctions();
$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($xmldoc);

CSS 选择示例

$xml = <<<EOB
<!DOCTYPE html>
<html lang="en">
    <head><title>stex css select demo</title></head>
    <body>
        <section class="first">
            <header>
                <h1 class="title">First section title</h1>
            </header>
            <article>Lorem ipsum...</article>
        </section>
        <section class="second">
            <header>
                <h1 class="title">Second section title</h1>
            </header>
            <article>Dolor it...</article>
        </section>
    </body>
</html>
EOB;
$xsl = <<<EOB
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
 <xsl:template match="/">

  <xsl:value-of select="@css['section.first .title']"></xsl:value-of>
  <xsl:text>&#xa;</xsl:text>

  <xsl:value-of select="@css['section.second .title']"></xsl:value-of>
  <xsl:text>&#xa;</xsl:text>

  <xsl:for-each select="@css['section article']">
      <xsl:value-of select="."></xsl:value-of>
      <xsl:text>&#xa;</xsl:text>
  </xsl:for-each>

 </xsl:template>
</xsl:stylesheet>
EOB;
$xmldoc = new \DOMDocument();
$xmldoc->loadXML($xml);
$xsldoc = new \DOMDocument();
$xsldoc->loadXML($xsl);

$proc = new StexXsltProcessor();
$proc->registerPHPFunctions();
$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($xmldoc);

其他示例

examples 目录中!

http://racztiborzoltan.github.io