XSLT2.0----<xsl:variable>和<xsl:sequence>

我们知道,<xsl:variable>可以定义变量,而且在使用content赋值时,会生成一个临时结果树。那么,是否content赋值,只能生成临时结果树呢?
XPATH2.0引入了sequence的概念,其实,变量也可以利用content定义为序列。
XML源文件:
<?xml version="1.0"?>
<
root>
    <branch>18</branch>
    <branch>13</branch>
    <branch>17</branch>
    <branch>90</branch>
<
/root>
我们可以利用如下XSLT文件:
<xsl:template match="/">
    <xsl:variable name="var" as="xs:integer">
        <xsl:sequence select="root/branch"/
>
    </xsl:variable
>
    <xsl:value-of select="$var" separator=","/>
<
/xsl:template>
将root下所有branch节点的文本组合成一个序列,并赋值给变量var。
那么这与
<xsl:template match="/">
    <xsl:variable name="var" >
        <xsl:value-of select="root/branch"/
>
    </xsl:variable
>
    <xsl:value-of select="$var" separator=","/>
<
/xsl:template>
有什么区别,我们有必要将变量赋值为序列吗?
第一段xslt代码生成一个序列,序列的每个元素都是xsl:integer类型。
第二段xslt代码生成一个临时结果树,临时结果树由一系列的文本节点组成。
序列相对于临时结果树有以下优点。
1,序列的生成和访问效率都要高于临时结果树。
2,临时结果树会失去数据的类型,因为一旦使用临时结果树,所有数据生成文本节点,文本节点的值的数据类型都是字符型。
 
但是我们运行第一段代码会报错:
A sequence of more than one item is not allowed as the value of variable $var
原因:
<xsl:variable>的as属性定义为xsl:integer,意为一个integer类型,这就要求<xsl:sequence>所生成的序列只能含有一个integer,但我们实际生成的是4 个integer。
修改办法:
<xsl:variable name="var" as="xs:integer*">
在xsl:integer后加一个星号,表示若干个integer元素。
<?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.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:rootfo:layout-master-set<fo:simple-page-master master-name=“A4"margin-top=“1in"margin-right=“1in"margin-bottom=“1in"margin-left=“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, serif’)[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(‘#’, replace(w:color/@w:val, ‘^FF’, ‘’))”/></xsl:attribute></xsl:if><xsl:if test=“w:b”><xsl:attribute name=“font-weight”>bold</xsl:attribute></xsl:if><xsl:if test=“w:i”><xsl:attribute name=“font-style”>italic</xsl:attribute></xsl:if><xsl:apply-templates select=“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:otherwisestart</xsl:otherwise></xsl:choose></xsl:attribute></xsl:template><!–段落缩进处理增强–><xsl:template match=“w:ind”><xsl:if test=“@w:firstLine”><xsl:attribute name=“text-indent”><xsl:value-of select=“concat(@w:firstLine div 20, ‘pt’)”/></xsl:attribute></xsl:if><xsl:if test=“@w:left”><xsl:attribute name=“margin-left”><xsl:value-of select=“concat(@w:left div 20, ‘pt’)”/></xsl:attribute></xsl:if></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”>start</xsl:attribute></xsl:attribute-set></xsl:stylesheet>修改这段样式表,注意语法问题,有一段提示语法错误,( $font-mapping/font[@w:name = $w-font]/@fo:name, $w-font, ‘SimSun, serif’ )[1]’ 中的语法错误。,一定要注意这是word的xml转xls-fo的xlst样式表,且xslt版本为3.0,请直接给出修改好的全部结果
最新发布
03-12
<?xml version=“1.0” encoding=“UTF-8”?> <xsl:stylesheet version=“3.0xmlns: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
<!-- 根模板 --> <xsl:template match="/"> <fo:root> <fo:layout-master-set> <fo:simple-page-master master-name="A4" page-width="210mm" page-height="297mm" margin-top="1cm" margin-bottom="1cm" margin-left="1cm" margin-right="3cm"> <fo:region-body margin-top="1cm" margin-bottom="1cm"/> <fo:region-before extent="1cm"/> <fo:region-after extent="1cm"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="A4"> <fo:flow flow-name="xsl-region-body" font-family="SimSun, 宋体" language="zh-CN" line-height="1.5"> <xsl:apply-templates select="//w:body"/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <!-- 正文处理 --> <xsl:template match="w:body"> <xsl:apply-templates select="*"/> </xsl:template> <!-- 段落处理 ★修改核心区域★ --> <xsl:template match="w:p"> <fo:block font-size="12pt" space-after="12pt" linefeed-treatment="ignore" wrap-option="wrap" text-align="justify"> <!-- 添加动态对齐控制 --> <xsl:choose> <!-- 检测段落属性中的居中设置 --> <xsl:when test="w:pPr/w:jc[@w:val='center']"> <xsl:attribute name="text-align">center</xsl:attribute> </xsl:when> <!-- 可扩展其他对齐方式 --> <xsl:when test="w:pPr/w:jc[@w:val='right']"> <xsl:attribute name="text-align">end</xsl:attribute> </xsl:when> </xsl:choose> <xsl:apply-templates select="w:r"/> </fo:block> </xsl:template> <!-- 文本运行处理(完整颜色支持版) --> <xsl:template match="w:r"> <fo:inline font-family="SimSun, 宋体, Microsoft YaHei, sans-serif"> <!-- 颜色处理必须放在最前面 --> <xsl:if test="w:rPr/w:color"> <xsl:attribute name="color"> <xsl:call-template name="convertWordColor"> <xsl:with-param name="wordColor" select="w:rPr/w:color/@w:val"/> </xsl:call-template> </xsl:attribute> </xsl:if> <!-- 粗体处理 --> <xsl:if test="w:rPr/w:b"> <xsl:attribute name="font-family">SimHei, 黑体</xsl:attribute> <xsl:attribute name="font-weight">bold</xsl:attribute> </xsl:if> <!-- 斜体处理 --> <xsl:if test="w:rPr/w:i"> <xsl:attribute name="font-family">KaiTi, 楷体</xsl:attribute> <xsl:attribute name="font-style">italic</xsl:attribute> </xsl:if> <!-- 实际文本内容 --> <xsl:value-of select="w:t"/> </fo:inline> </xsl:template> <!-- 颜色转换模板(XSLT 1.0兼容版) --> <xsl:template name="convertWordColor"> <xsl:param name="wordColor"/> <xsl:choose> <!-- 处理自动颜色 --> <xsl:when test="$wordColor = 'auto'">#000000</xsl:when> <!-- 处理8字符带透明度的情况(如FF0000FF) --> <xsl:when test="string-length($wordColor) = 8"> <xsl:value-of select="concat('#', substring($wordColor, 3, 6))"/> </xsl:when> <!-- 标准6字符HEX值 --> <xsl:when test="string-length($wordColor) = 6"> <xsl:value-of select="concat('#', $wordColor)"/> </xsl:when> <!-- 其他情况继承父级颜色 --> <xsl:otherwise>inherit</xsl:otherwise> </xsl:choose> </xsl:template>这个字体大小设置消失了,怎么修改,注意这是word.xmlxsl-fo的xslt脚本,且版本为1.0,请将修改好的代码提供出来
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值