日期:2014-05-16  浏览次数:20525 次

xslt 通过springmvc、js转换的方式
--》待翻译的xml文件内容
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<citizens>
	<citizen>
		<ssn>Z345T</ssn>
		<firstname>Cheryl</firstname>
		<lastname>Johnson</lastname>
		<role>Manager</role>
		<salary>12000</salary>
	</citizen>
	<citizen>
		<ssn>Z446T</ssn>
		<firstname>John</firstname>
		<lastname>Smith</lastname>
		<role>Employee</role>
		<salary>1000</salary>
	</citizen>
	<citizen>
		<ssn>Z335T</ssn>
		<firstname>Justin</firstname>
		<lastname>Claire</lastname>
		<role>Senior Manager</role>
		<salary>14000</salary>
	</citizen>
	<citizen>
		<ssn>Z389T</ssn>
		<firstname>Clark</firstname>
		<lastname>Rick</lastname>
		<role>Employee</role>
		<salary>2000</salary>
	</citizen>
</citizens>

--》xslt文件内容
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0">
	<xsl:output method="html" indent="yes" />
	<xsl:template match="/">
		<html>
			<body>
				<div align="center">
					<xsl:apply-templates />
				</div>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="citizens">
		<table border="1" width="100%">
			<xsl:for-each select="citizen">
				<tr>
					<td>
						<xsl:value-of select="ssn" />
					</td>
					<td>
						<xsl:value-of select="firstname" />
					</td>
					<td>
						<xsl:value-of select="lastname" />
					</td>
					<td>
						<xsl:value-of select="role" />
					</td>
					<td>
						<xsl:value-of select="salary" />
					</td>
				</tr>
			</xsl:for-each>
		</table>
	</xsl:template>
</xsl:stylesheet>

--》springmvc controller代码
package net.codejava.spring.controller;

//参考http://www.codejava.net/frameworks/spring/spring-mvc-xstlview-and-xsltviewresolver-example
import java.io.File;
import java.io.IOException;
import java.io.StringReader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {
	@RequestMapping(value = "/")
	public ModelAndView goHome(HttpServletResponse response) throws IOException {
		return new ModelAndView("home");
	}

	@RequestMapping(value = "/viewXSLT")
	public ModelAndView viewXSLT(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
/*		//从xml文件翻译
		String xmlFile = "resources/citizens.xml";
		String contextPath = request.getSession().getServletContext().getRealPath("");
		String xmlFilePath = contextPath + File.separator + "WEB-INF" + File.separator + xmlFile;
		Source source = new StreamSource(new File(xmlFilePath));*/
		//从xml字符串翻译
		String xmlStr = "<?xml version='1.0' encoding='UTF-8' standalone='no'?><citizens>	<citizen>		<ssn>Z345T</ssn>		<firstname>Cheryl</firstname>		<lastname>Johnson</lastname>		<role>Manager</role>		<salary>12000</salary>	</citizen>	<citizen>		<ssn>Z446T</ssn>		<firstname>John</firstname>		<lastname>Smith</lastname>		<role>Employee</role>		<salary>1000</salary>	</citizen>	<citizen>		<ssn>Z335T</ssn>		<firstname>