将参数从PHP发送到XSLT

时间:2015-02-05 01:18:03

标签: php xml url xslt

我试图向我的xsl发送一个参数,但我无法成功..我不明白为什么......我看了this link

我不认为我的XSL变量(nameArtist)具有我的PHP变量的值($ _GET [' name'],例如等于Brodinski)

我的档案是:

PHP文件:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="style.css" />
    <title>Zozor - Carnets de voyage</title>
</head>

<body>
<?php

        // Load XML file
        $xml = new DOMDocument;
        $xml->load('artists.xml');

        // Load XSL file
        $xsl = new DOMDocument;
        $xsl->load('artist.xsl');

    $process = new XSLTProcessor;
    $process->importStyleSheet($xsl);

    $process->setParameter('', 'nameArtist', $_GET['name'];);

    echo $process->transformToXML($xml);

?>
</body>

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="artist.rnc" type="application/relax-ng-compact-syntax"?>

<?xml-stylesheet type="text/xsl" href="artist.xsl"?>

<ArtistCollection>
    <artist>
        <name>Brodinski</name>
        <musicCategory>Electronic</musicCategory>
        <originalCountry>Germany</originalCountry>
        <photo>esfsdfsf</photo>
    </artist>

    <artist>
        <name>Louis Armstrong</name>
        <musicCategory>Jazz</musicCategory>
        <originalCountry>United States</originalCountry>
    </artist>

    <artist>
        <name>Miles Davis</name>
        <musicCategory>Jazz</musicCategory>
        <originalCountry>United States</originalCountry>
    </artist>
</ArtistCollection>

XSLT文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:param name="nameArtist" />
    <xsl:template match="/">

        <html>
            <body>
                <h2>Artists</h2>

                <p>salut <xsl:value-of select="$nameArtist" /></p>

                <xsl:for-each select="ArtistCollection/artist">

                    <table border="1">
                        <tr bgcolor="#9acd32">
                            <th>name</th>
                            <th>category</th>
                            <th>country</th>
                        </tr>
                        <tr>
                            <td><xsl:value-of select="name"/></td>
                            <td><xsl:value-of select="musicCategory"/>  </td>
                            <td><xsl:value-of select="originalCountry"/></td>
                        </tr>
                    </table>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

没有接受答案的老问题。原始代码基本上可以在这里工作 - &gt;阿帕奇/ PHP7。

我改变了半冒号,如前所述,以及样式表声明:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    exclude-result-prefixes="xsl"  version="1.0">  

以及php中的set param:

$process->setParameter('', 'nameArtist', "this value")

我建议问题是,或者简单地说 - &gt; $ _GET ['name']没有价值,或者服务器问题。