<xsl:result-document>指令用于输出内容到文件。
XML源文档:
<
?xml version="1.0" encoding="GB2312"?
>
< root >
< item f1= "浙江" f2= "绍兴" f3= "aaa" / >
< item f1= "江苏" f2= "苏州" f3= "bbb" / >
< item f1= "浙江" f2= "杭州" f3= "ccc" / >
< item f1= "山东" f2= "济南" f3= "ddd" / >
< item f1= "山东" f2= "青岛" f3= "aaa" / >
< item f1= "江苏" f2= "南京" f3= "fff" / >
< item f1= "山东" f2= "青岛" f3= "ggg" / >
< item f1= "江苏" f2= "南京" f3= "hhh" / >
< item f1= "山东" f2= "济南" f3= "iii" / >
< /root >
< root >
< item f1= "浙江" f2= "绍兴" f3= "aaa" / >
< item f1= "江苏" f2= "苏州" f3= "bbb" / >
< item f1= "浙江" f2= "杭州" f3= "ccc" / >
< item f1= "山东" f2= "济南" f3= "ddd" / >
< item f1= "山东" f2= "青岛" f3= "aaa" / >
< item f1= "江苏" f2= "南京" f3= "fff" / >
< item f1= "山东" f2= "青岛" f3= "ggg" / >
< item f1= "江苏" f2= "南京" f3= "hhh" / >
< item f1= "山东" f2= "济南" f3= "iii" / >
< /root >
XSLT文件:
<
?xml version='1.0'?
>
< xsl:stylesheet version= "2.0" xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" >
< xsl:output method= "html" encoding= "GB2312" name= "output" / >
< xsl:template match= "/" >
< xsl:for-each-group select= "root/item" group-by= "@f1" >
< xsl:sort select= "@f1" / >
< xsl:variable name= "filename" select= "concat('province',position())" / >
<xsl:result-document href="{$filename}.html" format="output" >
< xsl:value-of select= "concat(@f1,'-')" / >
< xsl:for-each-group select= "current-group()" group-by= "@f2" >
< xsl:sort select= "@f2" / >
< xsl:value-of select= "concat(' ',@f2,':')" / >
< xsl:for-each-group select= "current-group()" group-by= "@f3" >
< xsl:sort select= "@f3" / >
< xsl:value-of select= "@f3" / >
< xsl:if test= "position() != last()" >, < /xsl:if >
< /xsl:for-each-group >
< /xsl:for-each-group >
< /xsl:result-document >
< /xsl:for-each-group >
< /xsl:template >
< /xsl:stylesheet >
< xsl:stylesheet version= "2.0" xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" >
< xsl:output method= "html" encoding= "GB2312" name= "output" / >
< xsl:template match= "/" >
< xsl:for-each-group select= "root/item" group-by= "@f1" >
< xsl:sort select= "@f1" / >
< xsl:variable name= "filename" select= "concat('province',position())" / >
<xsl:result-document href="{$filename}.html" format="output" >
< xsl:value-of select= "concat(@f1,'-')" / >
< xsl:for-each-group select= "current-group()" group-by= "@f2" >
< xsl:sort select= "@f2" / >
< xsl:value-of select= "concat(' ',@f2,':')" / >
< xsl:for-each-group select= "current-group()" group-by= "@f3" >
< xsl:sort select= "@f3" / >
< xsl:value-of select= "@f3" / >
< xsl:if test= "position() != last()" >, < /xsl:if >
< /xsl:for-each-group >
< /xsl:for-each-group >
< /xsl:result-document >
< /xsl:for-each-group >
< /xsl:template >
< /xsl:stylesheet >
这里有几个问题需要注意的:
1,当用stylus studio 6.3的sax8.4来解析上面的XSLT文件时,出现错误:java.lang.RuntimeException:The System identifier of the principal output file is unkown.
错误的原因是,处理器不能很好的来解释XSLT2.0,并不是XSLT文件的语法问题。如果使用Stylus studio 2007的saxon8.7.3来解析,一切正常。
2,<xsl:result-document>的href属性要指定路径,那么相对路径跟哪个有关。实际的输出文件路径是相对当前XSLT文件的路径。
3,format属性指定输出的格式,该格式是上面预定义的:
<xsl:output method="html" encoding="GB2312" name="output"/>
如果没有指定输出编码为:GB2312,那输出的文件内容可能是乱码。
4,href="{$filename}.html"中的变量filename必须用大括号括起来。因为href属性的值是字符串,如果直接写成了href="$filename.html",则输出文件名为$filename.html。
这跟<xsl:value-of select="$filename">一类指令中的select属性不同,select属性就是用来指定节点集合(在XPATH2.0中理解为序列)或变量的。用大括号反而会出错。
5,如果href指定的文件名中含有汉字,则生成文件的文件名无法正常显示。这个问题留待解决。