XSLT <xsl:apply-templates> 元素

<xsl:apply-templates> 元素可把一个模板应用于当前的元素或者当前元素的子节点。


<xsl:apply-templates> 元素

<xsl:apply-templates> 元素可把一个模板应用于当前的元素或者当前元素的子节点。

假如我们向 <xsl:apply-templates> 元素添加一个 select 属性,此元素就会仅仅处理与属性值匹配的子元素。我们可以使用 select 属性来规定子节点被处理的顺序。

请看下面的 XSL 样式表:

实例

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="cd"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p> </xsl:template> <xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /> </xsl:template> <xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /> </xsl:template> </xsl:stylesheet>

<?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.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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值