XSLT之将XML文件转换为html、text、xml等文件类型

一、XML转换为html 

transform.xml

<?xml version="1.0" encoding="UTF-8"?>
<staff>
	<employee>
		<name>zhang3</name>
		<salary>100</salary>
		<hiredate year="1987" month="12" day="15"></hiredate>
	</employee>
	<employee>
		<name>li4</name>
		<salary>200</salary>
		<hiredate year="1986" month="11" day="14"></hiredate>
	</employee>
	<employee>
		<name>wang5</name>
		<salary>300</salary>
		<hiredate year="1985" month="10" day="13"></hiredate>
	</employee>
</staff>

样式表必须是.xsl格式的

makehtml.xsl

<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0">
		<xsl:output method="html"/>

		<xsl:template match="/staff">
			<table border="1"><xsl:apply-templates/></table>
		</xsl:template>

		<xsl:template match="/staff/employee">
			<tr><xsl:apply-templates/></tr>
		</xsl:template>

		<xsl:template match="/staff/employee/name">
			<td><xsl:apply-templates/></td>
		</xsl:template>

		<xsl:template match="/staff/employee/salary">
			<td><xsl:apply-templates/></td>
		</xsl:template>

		<xsl:template match="/staff/employee/hiredate">
			<td><xsl:value-of select="@year"/>-<xsl:value-of select="@month"/>-<xsl:value-of select="@day"/></td>
		</xsl:template>

	</xsl:stylesheet>

解释:

声明xsl

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

指定输出类型:可以是html、xml、text格式

<xsl:output method="html"/>

用<xsl:template>标签设置模版:

<xsl:template>

模板解释:

<xsl:template match="/staff">
			<table border="1"><xsl:apply-templates/></table>
		</xsl:template>

match属性是Xpath格式,有以下几点意思:
1.遇到staff标签后生成<table>标签并指定属性
2.<xsl:apply-templates/>说明继续对"子标签"应用相应模板(会自动向下寻找模板的)
3.当处理完所有子标签后,产生字符串</table>

属性值:

<xsl:template match="/staff/employee/hiredate">
			<td><xsl:value-of select="@year"/>-<xsl:value-of select="@month"/>-<xsl:value-of select="@day"/></td>
		</xsl:template>


<xsl:value-of 标签获取节点集的字符串值,节点集由"select"属性指定,"@属性值名称"

执行结果:

二、XML转换为.text

maketext.xsl
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0">
		<xsl:output method="text"/>

		<xsl:template match="/staff/employee">
			employee.<xsl:value-of select="position()"/>.name=<xsl:value-of select="name/text()"/>
			employee.<xsl:value-of select="position()"/>.salary=<xsl:value-of select="salary/text()"/>
			employee.<xsl:value-of select="position()"/>.hiredate=<xsl:value-of select="hiredate/@year"/>-<xsl:value-of select="hiredate/@month"/>-<xsl:value-of select="hiredate/@day"/>
		</xsl:template>

	</xsl:stylesheet>

transform.xml

<?xml version="1.0" encoding="UTF-8"?>
<staff>
	<employee>
		<name>zhang3</name>
		<salary>100</salary>
		<hiredate year="1987" month="12" day="15"></hiredate>
	</employee>
	<employee>
		<name>li4</name>
		<salary>200</salary>
		<hiredate year="1986" month="11" day="14"></hiredate>
	</employee>
	<employee>
		<name>wang5</name>
		<salary>300</salary>
		<hiredate year="1985" month="10" day="13"></hiredate>
	</employee>
</staff>

代码执行:

 //XML
        File file=new File("/work/transform.xml");
        StreamSource source=new StreamSource(file);

        //样式表
        File styleFile=new File("/work/maketext.xsl");
        StreamSource styleSource=new StreamSource(styleFile);

        TransformerFactory transformerFactory=TransformerFactory.newInstance();
        //此处是样式表的source参数,不是xml的source参数,要注意
        Transformer transformer=transformerFactory.newTransformer(styleSource);
        //此处是XML的source
        transformer.transform(source,new StreamResult(new FileOutputStream(new File("/work/text.text"))));

执行结果:

对 maketext.xsl 文件内容的解释

 

声明输出为.text格式

<xsl:output method="text"/>

以其父节点的角度来看当前节点的位置,和位置坐标类似,从1开始
position()

上述栗子得到的位置分别是2、4、6,而不是1、2、3,因为<employee>节点和<staff>父节点之间有空白元素,空白元素也算做XML的元素位置,去除空格就OK了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值