您的位置:首页 > 编程语言 > PHP开发

php5 xml学习2之xslt

2013-07-01 17:16 92 查看
看了下,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

[code]<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);
?>



[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: