一、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了。