[英]Using multiple xsl templates with one xml
我是xsl模板的新手,但尚未找到一种方法来做自己想要的事情。 看来这应该是可能的。
我想要的是为一个xml文件具有多个模板。 我正在做一个在线简历,所以我有我的resume.xml文件。 我想拥有我的onlineResume.xsl模板,我的downloadWord.xsl模板和我的downloadPDF.xsl模板。 这三个模板将根据在主页上选择的选项卡呈现。
现在,我将进行以下测试。 显然,它们的主体并没有充实,但是我不能不依赖于链接一个以上的样式表而定,具体取决于要遵循的链接。 我愿意接受php,javascript / jquery,xsl,xml,xpath或其他所需的工具。 我的网站是使用php在服务器端开发的。
HTML主页
<html>
<head></head>
<body>
<a href="./resume.xml">Online Resume</a>
<a href="./resume.xml">Download Word</a>
<a href="./resume.xml">Download PDF</a>
</body>
</html>
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="onlineCV.xsl"?>
<resume>
<personal_info>
<birthdate>07/08/1988</birthdate>
</personal_info>
<jobs>
<job>
<company>Radio Shack</company>
<title>Sales Representative</title>
<startdate>01/01/2011</startdate>
<enddate>02/02/2011</enddate>
<duration>1.5yrs</duration>
<sortdate>20110101</sortdate>
</job>
<job>
<company>Radio Shack2</company>
<title>Sales Representative</title>
<startdate>01/01/2013</startdate>
<enddate>02/02/2013</enddate>
<duration>1.5yrs</duration>
<sortdate>20130202</sortdate>
</job>
</jobs>
</resume>
在线简历
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Resume</h2>
<xsl:for-each select="resume/jobs/job">
<xsl:sort select="sortdate" data-type="number" order="descending" />
<div>
<xsl:value-of select="company" /> - <xsl:value-of select="title"/>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
下载Word
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Word Download</h2>
<xsl:for-each select="resume/jobs/job">
<xsl:sort select="sortdate" data-type="number" order="descending" />
<div>
<xsl:value-of select="company" /> - <xsl:value-of select="title"/>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
下载PDF
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My PDF Download</h2>
<xsl:for-each select="resume/jobs/job">
<xsl:sort select="sortdate" data-type="number" order="descending" />
<div>
<xsl:value-of select="company" /> - <xsl:value-of select="title"/>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
实际上,您可以使用不同的XSLT模板从XML生成不同的输出。 但是您不会生成不同的输出,而是将一个输出发送到浏览器并对其进行转换。
想一想。 目前,这里没有任何值/指标决定应使用哪个模板。
您有两种可能性:
$xsltFile = 'onlineCV.xsl';
$xmlDom = new DOMDocument();
$xmlDom->loadXml($xml);
$xslDom = new DOMDocument();
$xslDOM->load($xsltFile);
$processor = new XsltProcessor();
$processor->importStylesheet($xslDom);
echo $processor->transformToXml($xmlDom);
这将根据XSLT生成并输出并将其输出。
生成不同的XML输出,这些输出引用不同的XSLT模板并将其发送到浏览器
$xsltFile = 'onlineCV.xsl';
$dom = new DOMDocument();
$dom->loadXml($xml);
$dom->insertBefore(
$dom->createProcessingInstruction(
'xml-stylesheet',
'type="text/xsl" href="'.$xsltFile.'"'
),
$dom->documentElement
);
echo $dom->saveXml();
演示: https : //eval.in/162137
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.