播客 > 玩代码  >  asp/php使用Xslt转换Xml  | 登录  | RSS订阅地址  | Code平台

asp/php使用Xslt转换Xml

 使用ASP处理XSLT转换XML比较简单,思路如下:创建一个XSLTemplate的对象,再创建一个XMLDOM对象,然后在家Xml文件和XSLT文件,最后使用方法transform即可,包含到类里面,具体代码如下。
Class Cls_Xml_Transform
    Private lInput,XSLTemplate
    Private p_Output
    Public Property Get Output()
        Output = p_Output
    End Property
    Private Property Let Output(ByVal strInfo)
        p_Output = strInfo
    End Property

    Public Property Let Input(ByVal vNewValue)
        If IsObject(vNewValue) Then Set lInput=vNewValue
    End Property

    Public Property Let XSLTemplatefile(ByVal vNewValue)
        Dim StyleSheet
        Dim vNewValue_

        vNewValue_ = vNewValue

        If Not InStr(vNewValue,":\") > 0 Then
            vNewValue  = Server.MapPath(vNewValue)
        End If

        Set XSLTemplate=Server.CreateObject("Msxml2.XSLTemplate")
        Set StyleSheet=Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
        StyleSheet.load vNewValue
        XSLTemplate.StyleSheet=StyleSheet
    End Property

    Public Sub Transform()
        Dim proc
        Set proc = XSLTemplate.createProcessor()
        proc.input=linput
        proc.transform()
        Output=proc.output
        Set proc=Nothing
    End Sub

End Class


使用范例:
Set XMLDOM = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
XMLDOM.async = false
XMLDOM.load(Server.MapPath("bi2.xml"))
Set o=new Cls_IO_Transform
o.XSLTemplatefile="bi2.xsl"
o.Input=XMLDOM
o.Transform()
response.write o.Output()


这里处理的直接是XmlDom对象了,如果需要,灵活可以修改。


 使用PHP处理XSLT解析XML,在PHP5以后的版本中,支持的非常好了。首先要修改你的PHP.INI,让支持DomDocument,xsltprocessor这2个类即可,具体代码如下:
<?php
header("Content-Type: text/html; charset=utf-8");
$xml =new DomDocument;
$xml->load("test.xml");
$xsl =new DomDocument;   
$xsl->load("test.xsl");   
$proc=new xsltprocessor;
$proc->importStyleSheet($xsl);
/*
//1:输出解析结果

$outdoc = $proc->transformToDoc($xml);
echo $outdoc->saveXML();
*/
//2:解析成HTML
echo $proc->transformToXML($xml);
/*

*/
/*
//3:输出文本
echo trim($proc->transformToDoc($xml)->firstChild->wholeText);
*/
/*
//4:生成文件
*/
$proc->transformToURI($xml, 'test.html');

?>
如上,每种情况对亚的每种结果的获取方式。

天气:大雨,ccdot发表于2007-10-10 13:46:19,阅读了888次,共有个0回复.

暂无评论

  1. 想要转载我文章的人滚远远的,能想多远,就滚多远。
  2. 不要提交任何带有网址URL信息的评论.
  3. 需要更多信息?请使用站内搜索,郁闷了?听听我在听什么吧!
用户名:*验证:看不清楚请点击刷新验证码*
内容: