使用模板匹配选择相册

时间:2015-11-11 22:55:58

标签: php xslt

我创建了一个下拉选择框,用于选择要查看的相册。当用户进行选择时,他们然后单击“查看相册”,他们所做选择的ID将通过POST发送到查看页面:select_album.php

从数据库中查询所有相册的列表,并显示在下拉选项中。每张专辑都有一个与之关联的ID。根据选择的相册,当用户点击“查看相册”时,会将其发送到select_album.php,其中会显示该相册的信息。

下拉列表中的默认选择是[SELECT ALBUM],仅用作占位符。我的问题是,当选择[SELECT ALBUM]并单击View Album时,用户将被发送到select_album.php并看到一个空白页面。我需要页面来显示专辑信息:没有艺术家:没有专辑标题 标签:NO LABEL,0 TRACKS等。我将显示一个没有专辑封面的专辑图片。

我认为我需要做的是以某种方式使模板匹配,但我很困惑从哪里开始。

This is the dropdown This is example of a regular album page

select_album.php

<?php
$sql = "SELECT * from albums ORDER BY artist";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
        // output data of each row

?>  
<form method='POST' action='write_xml.php'><b>SELECT AN ALBUM TO VIEW:</b><br />
<select name='id' size=1>
<option value=''>[SELECT ALBUM]</option>
<?php
        while($row = $result->fetch_assoc()) {
?>  
<option value="<?php echo $row['id']; ?>"><?php echo $row['artist']. ' - '   .$row['name']; ?></option>
<?php
        }
        } else {
        echo "0 results";
        }
        $conn->close();
?>
</select><br /><br />

<input type='submit' name='process' value='VIEW ALBUM' />

music_inventory.xsl

<xsl:stylesheet  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" >
<xsl:output method="xml" />

<xsl:param name="albumid">JA1</xsl:param>
<xsl:template match="/">

    <html>
        <body>
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>
<xsl:template match="music_inventory">
    <xsl:apply-templates select="album[@id=$albumid]"/>
</xsl:template>
<!--creates hyperlink-->
<xsl:template match="album">
 <img src="{@albumart}" style="position: absolute; left: 10px; top: 10px; width: 200px;" />

    <div style="position: absolute; left: 215px; font-size: 16pt; color: #993333;">

        <b><xsl:value-of select="artist"/>: <a xmlns="http://www.w3.org/1999/xhtml" href="{@id}.xhtml">
            <xsl:value-of select="name"/>
        </a></b>

    </div>
     <div style="position: absolute; left: 215px; top: 40px; font-size: 10pt;">
   <b>Label: </b><xsl:value-of select="label"/><br />
   <b>Year: </b><xsl:value-of select="year"/><br />
   <b>Disc <xsl:value-of select="disc"/> of <xsl:value-of select="totaldiscs"/></b><br />
 </div>

 <div style="position: absolute; left: 215px; top: 100px; font-size: 12pt;">
   <b>Track Listing:</b><br/>
   <xsl:for-each select="tracklist/*">
      <xsl:apply-templates select="@id"/>: <xsl:value-of select="."/><br />
   </xsl:for-each>
 </div>

</xsl:template>

write_xml.php

$query="select * from albums";
$result=mysqli_query($db,$query);

$fp=fopen('music_inventory.xml','w');
$header="<?xml version=\"1.0\"?>\n<!DOCTYPE album SYSTEM   \"music_inventory.dtd\">\n";
$header.="<?xml-stylesheet type=\"text/css\" href=\"music_inventory.css\"?>\n";
$header.="<music_inventory>\n";
$written=fwrite($fp,$header);

$xml_data="";


while($row=mysqli_fetch_array($result))

{
    // Insert code here to write the outer album info
    $type=null;
$xml_data .= "<album id=\"".$row['id']."\" type=\"".$type."\"   albumart=\""   .$row['albumart']."\">";
$xml_data .= "<artist>".htmlentities($row['artist'])."</artist>\n";
$xml_data .= "<name>".$row['name']."</name>\n";
$xml_data .= "<year>".$row['year']."</year>\n";
$xml_data .= "<label>".$row['label']."</label>\n";
$xml_data .= "<disc>".$row['disc']."</disc>\n";
$xml_data .= "<totaldiscs>".$row['totaldiscs']."</totaldiscs>\n";



    switch($row["type"])
    {
            case 'E':
                    $type='EP';
                    break;
            case 'F':
                    $type='full_length';
                    break;
            case 'S':
                    $type='soundtrack';
                    break;
            case 'C':
                    $type='compilation';
                    break;
            case 'M':
                    $type='multi_disc';
                    break;
            default:
                    $type='unknown';
                    break;
    }


            $xml_data .= "<tracklist>\n";      


    $track_query="select track_number,track_title,track_artist from tracklist where id='" . $row["id"] . "' order by track_number";
    $track_result=mysqli_query($db,$track_query);
    while($track_row=mysqli_fetch_array($track_result))
    {
            // Insert code here to write the inner track list info
            $xml_data .= "<track id=\"".$track_row['track_number']."\">".htmlentities($track_row['track_title'])."</track>\n";

    }


    $xml_data .= "</tracklist>\n";
    $xml_data .= "</album>\n";

}





$written=fwrite($fp,$xml_data);
$footer="</music_inventory>\n";
$written=fwrite($fp,$footer);
fclose($fp);

$xslDoc = new DOMDocument();
$xslDoc->load("music_inventory.xsl");

$xmlDoc = new DOMDocument();
$xmlDoc->load("music_inventory.xml");

$proc = new XSLTProcessor();
$proc->importStylesheet($xslDoc);

$proc->setParameter('', 'albumid', $_POST["id"]);

echo $proc->transformToXML($xmlDoc);

?>

1 个答案:

答案 0 :(得分:0)

在您的XSLT中,您当前正在为相册选择模板,如下所示:

<xsl:template match="music_inventory">
   <xsl:apply-templates select="album[@id=$albumid]"/>
</xsl:template>

如果您想处理$albumid为空的情况,可以在此之后添加xsl:if条件。

<xsl:template match="music_inventory">
   <xsl:apply-templates select="album[@id=$albumid]"/>
   <xsl:if test="$albumid = ''">
       <p>NO ALBUM</p>
   </xsl:if>
</xsl:template>

或者可能是这样,处理$albumid设置但不存在的情况:

<xsl:template match="music_inventory">
   <xsl:variable name="$album" select="album[@id=$albumid]" />
   <xsl:apply-templates select="$album"/>
   <xsl:if test="$album">
       <p>NO ALBUM</p>
   </xsl:if>
</xsl:template>

当然,另一种解决方案是简单地禁用&#34;查看相册&#34;每当&#34; SELECT ALBUM&#34;被选中,以避免不必要的回发。