XSLT中<xsl:apply-templates>的使用方式

XSLT 是一种用于将 XML 文档转换为 XHTML 文档或其他 XML 文档的语言。这种转换通常由专用的软件或者浏览器执行。目前,绝大多数主流的浏览器支持XSLT转换。XSLT本质上也是一个XML文档,因此它的编写也是要遵循XML的基本规范。XSLT中有很多元素,此处总结一下<xsl:apply-templates>的使用方法。

假设有如下一个XML文档。

其中加粗的部分,是为了能够使得浏览器能够转换该XML文档而引入指令语句,表示引入一个example.xslt文件

image

那么任务的重点实际上是编辑符合规范的example.xsl文件。我们一步一步完善这个XSLT文件。首先加入 <xsl:template match="/">

节点,如下:

image

该节点中,match="/"表示匹配需转换的xml文件中的根结点,这是一个XPath的路径表达式,(XPath 是一门在 XML 文档中查找信息的语言。XPath 可用来在 XML 文档中对元素和属性进行遍历,如果需要了解更多关于XPath的信息,可以参考http://www.w3school.com.cn/xpath/index.asp)。因此 <xsl:template match="/"> </xsl:template> 也就表示了把需要转换的XML文档的根结点内的内容换成 <xsl:template match="/"> </xsl:template> 中的内容。即转换后变成了:

 <html>

  <body>

<h2>职员列表清单</h2>

  </body>

 </html>

因此在IE中显示的内容如下:

fbf803f755d94b9bbb1a431efb4c2fb2

接下来可以使用<xsl:apply-templates>,<xsl:apply-templates>类似一个函数,对 <xsl:template match="/"> 中的每一个元素做一次转换。 <xsl:apply-templates> 总是包含在  <xsl:template >中的。

如果在XSLT中加了<xsl:apply-templates>,即如下:

image

那么,默认的显示了当前template中的根元素的值,因为当前template中的match是"/",对应的内容是XML文档中的<company>节点的内容,因此把company节点下的元素值都显示了,如下:

0ca7d1da36d243fbb3236d8245386aa7

这类直接获取了company节点下的值,并且也没指定要如何转换,因此等价于下面的写法:

image

如果只要获取staff的值,那么在select的时候,指定staff节点既可以了。

image

或者,也可以同时修改template的match值:

image

显示的内容都是一样的,如下图:

2b68ef8d2d3746029220d8ea53e7bb71

事实上,上例中,我们定义了模板,改模板只要求匹配"/"即根结点元素,对于根结点内的子元素,如何转换并无响应的模板来指明,因此下一步,在定义一个新的模板,来指明如何对staff元素实行转换:

image

此处添加了一个新的模板,这个模板只对staff元素生效。因此,在上面<xsl:apply-templates select="company/staff"/>应用这个模板时,检测到下面有对应的模板,就会根据该模板来转换当前的元素。因此显示效果如下:

3c854f786418484da453d4f1ca6d24a1
也就是说,相当于把XML转换成了如下的html代码:

<html>

<body>

  <h2>职员列表清单</h2>

  <b>姓名:张三</b>: 性别:male年龄:30

  <b>姓名:李四</b>: 性别:female年龄:22

  </body>

</html>

更完善的,我们可以再添加一个模板,显示老板的信息,如下:

image

转换结果如下:

01196919cf4b449cbe991c2ca50c3514

至此, <xsl:apply-templates />的使用方法也有大致的解了,总结一下:

<xsl:apply-templates/>总是包含在<xsl:template />中,当转换软件遇到这个语句时,就会对 <xsl:apply-templates/>所匹配的元素进行转换(所匹配的元素由select属性设置,该值是一个XPath中的路径表达式,并且和<xsl:template />中的match相关)。

<xsl:apply-templates/>在对元素应用模板的时候,会去文档中查看是否存在一个对应的模板(即该模板定义了如何转换 <xsl:apply-templates/>匹配的元素)。所谓处理转换,实际上就是把XML文档中的值,代入模板,并输出代入后的模板内容,类似变量赋值。

网上看到一篇文章http://www.cnblogs.com/it_mac/archive/2010/06/27/1766151.html,作者列出了他的错误。实际上,错误是由于第18行<xsl:apply-templates/>,该语句的执行,是在<xsl:template match="cd">下,即在对cd下的元素做转换的时候。cd下有3个子元素title,artist和tracks,这3个子元素都会做转换,只不过由于并未找到和title,artist元素相对应的模板,因此把他们按文本的形式输出了,所以转换的结果就如作者写的那样。

这里只需要做一个小改动,把语句改成<xsl:apply-templates select="tracks"/>即可,这表示,对cd下的元素,只选择tracks元素做转换,就解决问题了。

<?xml version="1.0"encoding="UTF-8"?><xsl:stylesheet version="3.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:fo="http://www.w3.org/1999/XSL/Format"xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"exclude-result-prefixes="w"><!--增强字体映射--><xsl:variable name="font-mapping"><font w:name="宋体"fo:name="SimSun"/><font w:name="黑体"fo:name="SimHei"/><font w:name="等线"fo:name="DengXian"/></xsl:variable><!--根模板--><xsl:template match="/"><fo:root><fo:layout-master-set><fo:simple-page-master master-name="A4"margin="1in"><fo:region-body margin-top="0.5in"margin-bottom="0.5in"/><fo:region-before extent="0.5in"/><fo:region-after extent="0.5in"/></fo:simple-page-master></fo:layout-master-set><fo:page-sequence master-reference="A4"><fo:flow flow-name="xsl-region-body"><xsl:apply-templates select="//w:body/*"/></fo:flow></fo:page-sequence></fo:root></xsl:template><!--增强段落处理--><xsl:template match="w:p"><fo:block xsl:use-attribute-sets="paragraph-style"><xsl:apply-templates select="w:pPr/w:jc"mode="align"/><xsl:apply-templates select="w:pPr/w:ind"/><xsl:apply-templates select=".//w:r"/></fo:block></xsl:template><!--文本格式处理增强--><xsl:template match="w:r"><fo:inline><xsl:apply-templates select="w:rPr"/><xsl:value-of select="string-join(w:t, '')"/></fo:inline></xsl:template><!--增强字体处理--><xsl:template match="w:rPr"><xsl:variable name="w-font"select="(w:rFonts/@w:ascii, w:rFonts/@w:hAnsi)[1]"/><xsl:attribute name="font-family"><xsl:value-of select="($font-mapping/font[@w:name = $w-font]/@fo:name, $w-font, 'SimSun')[1]"/></xsl:attribute><xsl:if test="w:sz/@w:val"><xsl:attribute name="font-size"><xsl:value-of select="concat(w:sz/@w:val * 0.5, 'pt')"/></xsl:attribute></xsl:if><xsl:if test="w:color/@w:val != 'auto'"><xsl:attribute name="color"><xsl:value-of select="concat('#', w:color/@w:val)"/></xsl:attribute></xsl:if><xsl:apply-templates select="w:b | w:i | w:u | w:strike"/></xsl:template><!--下划线和删除线处理--><xsl:template match="w:u"><xsl:attribute name="text-decoration">underline</xsl:attribute></xsl:template><xsl:template match="w:strike"><xsl:attribute name="text-decoration">line-through</xsl:attribute></xsl:template><!--表格处理增强--><xsl:template match="w:tbl"><fo:table table-layout="fixed"width="100%"><xsl:for-each select="w:tblGrid/w:gridCol"><fo:table-column column-width="{@w:w div 20}pt"/></xsl:for-each><fo:table-body><xsl:apply-templates select="w:tr"/></fo:table-body></fo:table></xsl:template><xsl:template match="w:tr"><fo:table-row><xsl:apply-templates select="w:tc"/></fo:table-row></xsl:template><xsl:template match="w:tc"><fo:table-cell border="1pt solid #000"padding="4pt"><fo:block><xsl:apply-templates select=".//w:p"/></fo:block></fo:table-cell></xsl:template><!--段落对齐处理--><xsl:template match="w:jc"mode="align"><xsl:attribute name="text-align"><xsl:choose><xsl:when test="@w:val = 'center'">center</xsl:when><xsl:when test="@w:val = 'right'">end</xsl:when><xsl:when test="@w:val = 'both'">justify</xsl:when><xsl:otherwise>start</xsl:otherwise></xsl:choose></xsl:attribute></xsl:template><!--段落缩进处理--><xsl:template match="w:ind"><xsl:attribute name="text-indent"><xsl:value-of select="concat(@w:firstLine div 20, 'pt')"/></xsl:attribute></xsl:template><!--增强段落样式--><xsl:attribute-set name="paragraph-style"><xsl:attribute name="space-after">12pt</xsl:attribute><xsl:attribute name="line-height">1.5</xsl:attribute><xsl:attribute name="text-align">left</xsl:attribute></xsl:attribute-set></xsl:stylesheet>检查这个xlst样式表的问题,一定要注意这是word的xml转xls-fo的xlst样式表,且xslt版本为3.0,请直接给出修改好的全部结果
最新发布
03-12
<?xml version=“1.0” encoding=“UTF-8”?> <xsl:stylesheet version=“3.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform” xmlns:fo=“http://www.w3.org/1999/XSL/Format” xmlns:w=“http://schemas.openxmlformats.org/wordprocessingml/2006/main” exclude-result-prefixes=“w”> <!-- 根模板 --> <xsl:template match="/"> <fo:root> <fo:layout-master-set> <fo:simple-page-master master-name="A4"> <fo:region-body margin="1in"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="A4"> <fo:flow flow-name="xsl-region-body"> <xsl:apply-templates select="//w:body"/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <!-- 段落处理 --> <xsl:template match="w:p"> <fo:block xsl:use-attribute-sets="paragraph-style"> <xsl:apply-templates select=".//w:r"/> </fo:block> </xsl:template> <!-- 文本格式处理 --> <xsl:template match="w:r"> <fo:inline> <xsl:apply-templates select="w:rPr"/> <xsl:value-of select="w:t"/> </fo:inline> </xsl:template> <!-- 字体样式提取 --> <xsl:template match="w:rPr"> <xsl:if test="w:rFonts/@w:ascii"> <xsl:attribute name="font-family" select="w:rFonts/@w:ascii"/> </xsl:if> <xsl:if test="w:sz/@w:val"> <xsl:attribute name="font-size" select="concat(w:sz/@w:val * 0.5, 'pt')"/> </xsl:if> <xsl:if test="w:color/@w:val"> <xsl:attribute name="color" select="concat('#', w:color/@w:val)"/> </xsl:if> </xsl:template> <!-- 加粗处理 --> <xsl:template match="w:b"> <xsl:attribute name="font-weight">bold</xsl:attribute> </xsl:template> <!-- 斜体处理 --> <xsl:template match="w:i"> <xsl:attribute name="font-style">italic</xsl:attribute> </xsl:template> <!-- 表格处理 --> <xsl:template match="w:tbl"> <fo:table table-layout="fixed" width="100%"> <xsl:apply-templates select="w:tr"/> </fo:table> </xsl:template> <xsl:template match="w:tr"> <fo:table-row> <xsl:apply-templates select="w:tc"/> </fo:table-row> </xsl:template> <xsl:template match="w:tc"> <fo:table-cell border="1pt solid black" padding="2pt"> <fo:block> <xsl:apply-templates select=".//w:p"/> </fo:block> </fo:table-cell> </xsl:template> <!-- 段落默认样式 --> <xsl:attribute-set name="paragraph-style"> <xsl:attribute name="space-after">12pt</xsl:attribute> <xsl:attribute name="text-align">left</xsl:attribute> </xsl:attribute-set> </xsl:stylesheet>完善修改这个xslt样式表,一定要注意这是word的xml转xls-fo的xlst样式表,且xslt版本为3.0,请直接给出修改好的全部结果
03-11
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" exclude-result-prefixes="w"> <!-- 根模板 --> <xsl:template match="/"> <fo:root> <fo:layout-master-set> <fo:simple-page-master master-name="A4"> <fo:region-body margin="1in"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="A4"> <fo:flow flow-name="xsl-region-body"> <xsl:apply-templates select="//w:body"/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <!-- 段落处理 --> <xsl:template match="w:p"> <fo:block xsl:use-attribute-sets="paragraph-style"> <xsl:apply-templates select=".//w:r"/> </fo:block> </xsl:template> <!-- 文本格式处理 --> <xsl:template match="w:r"> <fo:inline> <xsl:apply-templates select="w:rPr"/> <xsl:value-of select="w:t"/> </fo:inline> </xsl:template> <!-- 字体样式提取 --> <xsl:template match="w:rPr"> <xsl:if test="w:rFonts/@w:ascii"> <xsl:attribute name="font-family" select="w:rFonts/@w:ascii"/> </xsl:if> <xsl:if test="w:sz/@w:val"> <xsl:attribute name="font-size" select="concat(w:sz/@w:val * 0.5, 'pt')"/> </xsl:if> <xsl:if test="w:color/@w:val"> <xsl:attribute name="color" select="concat('#', w:color/@w:val)"/> </xsl:if> </xsl:template> <!-- 加粗处理 --> <xsl:template match="w:b"> <xsl:attribute name="font-weight">bold</xsl:attribute> </xsl:template> <!-- 斜体处理 --> <xsl:template match="w:i"> <xsl:attribute name="font-style">italic</xsl:attribute> </xsl:template> <!-- 表格处理 --> <xsl:template match="w:tbl"> <fo:table table-layout="fixed" width="100%"> <xsl:apply-templates select="w:tr"/> </fo:table> </xsl:template> <xsl:template match="w:tr"> <fo:table-row> <xsl:apply-templates select="w:tc"/> </fo:table-row> </xsl:template> <xsl:template match="w:tc"> <fo:table-cell border="1pt solid black" padding="2pt"> <fo:block> <xsl:apply-templates select=".//w:p"/> </fo:block> </fo:table-cell> </xsl:template> <!-- 段落默认样式 --> <xsl:attribute-set name="paragraph-style"> <xsl:attribute name="space-after">12pt</xsl:attribute> <xsl:attribute name="text-align">left</xsl:attribute> </xsl:attribute-set> </xsl:stylesheet>完善修改这个xslt样式表,一定要注意这是word的xml转xls-fo的xlst样式表,且xslt版本为3.0,请直接给出修改好的全部结果
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值