样式Feed内容以XML格式返回

时间:2011-01-23 03:13:36

标签: php xml dom xslt

我有这个代码从我的数据库中提取信息,然后根据API检查它。我正在尝试将返回信息自定义到表中。我会附上一张图片来展示我想要的东西。我到处搜索,我不知道如何编码。不要担心CSS只是让信息填满表格。

<?php
        $key = "********************";
        $address = urlencode($CustomFields->field('jr_address',$listing,false,false));
        $city = $listing['Category']['title'];
        $zip = $CustomFields->field('jr_zipcode',$listing,false,false);
        $url = "http://api.greatschools.org/schools/nearby?key={$key}&address={$address}&city={$city}&state=MI&zip={$zip}&schoolType=public-charter&levelCode=elementary-schools&minimumSchools=50&radius=10&limit=5";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($curl);      
        curl_close($curl); 
        print_r($response);
?><style type='text/css'>gsId{display:none;}name{color:#9BB055; font-size:18px;}type{display:none;}gradeRange{}enrollment{}gsRating{}city{display:none;}state{display:none;}district{}districtNCESId{display:none;}address{display:none;}phone{display:none; }fax{display:none;}ncesId{display:none;}lat{display:none;}lon{display:none;}</style>

退货代码:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<schools>
<school>
<gsId>6350</gsId>
<name>Chinese Education Center</name>
<type>public</type>
<gradeRange>K-5</gradeRange>
<enrollment>63</enrollment>
<gsRating>3</gsRating>
<city>San Francisco</city>
<state>CA</state>
<district>San Francisco Unified School District</district>
<districtNCESId>0634410</districtNCESId>
<address>657 Merchant St., San Francisco, CA  94111</address>
<phone>(415) 291-7918</phone>
<fax>(415) 291-7965</fax>
<ncesId>063441005596</ncesId>
<lat>37.795</lat>
<lon>-122.4042</lon>
<overviewLink>http://www.greatschools.org/california/san-francisco/6350-Chinese-Education-Center/?s_cid=gsapi</overviewLink>
<ratingsLink>http://www.greatschools.org/school/rating.page?state=CA&amp;id=6350&amp;s_cid=gsapi</ratingsLink>
<reviewsLink>http://www.greatschools.org/school/parentReviews.page?state=CA&amp;id=6350&amp;s_cid=gsapi</reviewsLink>
</school>
<school>
<gsId>6389</gsId>
<name>Gordon J. Lau Elementary School</name>
<type>public</type>
<gradeRange>K-5</gradeRange>
<enrollment>667</enrollment>
<gsRating>7</gsRating>
<city>San Francisco</city>
<state>CA</state>
<district>San Francisco Unified School District</district>
<districtNCESId>0634410</districtNCESId>
<address>950 Clay St., San Francisco, CA  94108</address>
<phone>(415) 291-7921</phone>
<fax>(415) 291-7952</fax>
<website>http://www.gjles.org/</website>
<ncesId>063441005599</ncesId>
<lat>37.794</lat>
<lon>-122.4086</lon>
<overviewLink>http://www.greatschools.org/california/san-francisco/6389-Gordon-J.-Lau-Elementary-School/?s_cid=gsapi</overviewLink>
<ratingsLink>http://www.greatschools.org/school/rating.page?state=CA&amp;id=6389&amp;s_cid=gsapi</ratingsLink>
<reviewsLink>http://www.greatschools.org/school/parentReviews.page?state=CA&amp;id=6389&amp;s_cid=gsapi</reviewsLink>
</school>
</schools>

我不知道如何编写脚本使其看起来像这样: 链接到图像: link text


所以新文件看起来像这样?

<?php
    $key = "xxxxxxxxxxxxxxxxxxxxxxxx";
    $address = urlencode($CustomFields->field('jr_address',$listing,false,false));
    $city = $listing['Category']['title'];
    $zip = $CustomFields->field('jr_zipcode',$listing,false,false);
    $url = "http://api.greatschools.org/schools/nearby?key={$key}&address={$address}&city={$city}&state=MI&zip={$zip}&schoolType=public-charter&levelCode=elementary-schools&minimumSchools=50&radius=10&limit=5";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($curl);      
    curl_close($curl);
    echo (xml2html($response, "/xslt/schools.xsl");

&GT;

我在回声线上出错了。

这是怎么回事?

<?php
function xml2html($xmldata, $xslPath)
{
    /* $xmldata -> your XML */
    /* $xsl -> XSLT file */

    $arguments = array('/_xml' => $xmldata);
    $xsltproc = xslt_create();
    xslt_set_encoding($xsltproc, 'ISO-8859-1');
    $html =
        xslt_process($xsltproc, 'arg:/_xml', $xslPath, NULL, $arguments);

    if (empty($html)) {
       die('XSLT processing error: '. xslt_error($xsltproc));
    }
    xslt_free($xsltproc);
    return $html;
}
?>

1 个答案:

答案 0 :(得分:0)

浏览器无法可视化自定义XML(即您的学校xml)。 所以你应该手动将它转换为HTML。我知道2个解决方案:

  1. 使用xml parser functions
  2. 解析XML
  3. 通过XSLT表格转换XML(对我而言,更好的方法)。
  4. 对于提供的XML我已经编写了下一个XSLT:link text(对不起外部链接,我在这里粘贴XSLT代码时遇到无法解释的麻烦)

    转换可以由函数xslt_process应用。 (您应该将xsl文件存储在服务器文件夹中的某个位置(例如“xslt / schools.xsl”)并转换xml响应):

    <?php
    function xml2html($xmldata, $xslPath)
    {
        /* $xmldata -> your XML */
        /* $xsl -> XSLT file */
    
        $arguments = array('/_xml' => $xmldata);
        $xsltproc = xslt_create();
        xslt_set_encoding($xsltproc, 'ISO-8859-1');
        $html =
            xslt_process($xsltproc, 'arg:/_xml', $xslPath, NULL, $arguments);
    
        if (empty($html)) {
           die('XSLT processing error: '. xslt_error($xsltproc));
        }
        xslt_free($xsltproc);
        return $html;
    }
    
    // making request...
    $response = curl_exec($curl);      
    curl_close($curl);
    echo(xml2html($response, "/xslt/schools.xsl"));
    

    &GT;