php5 xml学习2之xslt

44 篇文章 0 订阅
36 篇文章 0 订阅

看了下,php5的XSLT十分简单,举例子说明之.

首先是XML

<?xml version='1.0' ?>
<contacts>
<contact idx="37">
<name>Ramsey White II</name>
<category>Family</category>
<phone type="home">301-555-1212</phone>
<meta id="x634724" />
</contact>
<contact idx="42">
<name>Stratis Kakadelis</name>
<category>Friends</category>
<phone type="home">240-555-1212</phone>
<phone type="work">410-555-7676</phone>
<email>skak@example.com</email>
<meta id="y49302" />
</contact>
<contact idx="57">
<name>Kelly Williamson</name>
<category>Friends</category>
<phone type="cell">443-555-9999</phone>
<email>kwill@example.com</email>
<email>dynky@tech.example.com</email>
<meta id="w4r302" />
</contact>
</contacts>
 
之后是XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="contacts">
<html><head><title>Contacts!</title></head><body>
<div style="border: 2px solid blue; padding: 5px;">
<h1>Contacts:</h1>
<xsl:apply-templates />
</div></body></html>
</xsl:template>
<xsl:template match="contact">
<div style="border: 1px solid black; margin: 20px; padding: 5px;">
<h2><xsl:value-of select="name" /></h2>
<p>
Home Phone: <xsl:value-of select="phone[@type='home']" /><br />
Work Phone: <xsl:value-of select="phone[@type='work']" /><br />
Cell Phone: <xsl:value-of select="phone[@type='cell']" /><br />
</p>
</div>
</xsl:template>
</xsl:stylesheet>
最后是调用的php,这里用的是dom
<?php
// Using the DOM extension, load the XML file into memory:
$dom = new DOMDocument();
$dom->load('contacts.xml');
// Now also load the XSL file as well:
$xsl = new DOMDocument();
$xsl->load('contacts.xsl');
// Create a new XSLT Processor
$proc = new XSLTProcessor;
// Import the XSL styles into this processor
$proc->importStyleSheet($xsl);
// Now transform the XML file and echo it to the screen!
echo $proc->transformToXML($dom);
?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值