Crazy.Coder --- have more fun in coding

世界上唯有两样东西能使我们的心灵受到深深的震撼,一是我们头顶上璀灿的星空,另一个便是人们内心深处的道德法则。 -- 康德
2 tips, about infopath and xslt

在用Infopath 2003设计好表单后,用户的输入是很方便了,但是对于没有装Infopath 2003客户端的用户来说,浏览起来不是很方便。在Office 2007之前,用XSLT来转换Infopath的XML文件也不失为一个好的解决之道。

 

这几天在写XSLT中碰到了两个问题,写出来给大家共享一下。

1、XML输出为HTML后文字的格式丢失

问题:Infopath表单上用Richtext编辑的带有格式的文字用<xsl:value-of>输出为HTML后格式全部丢失

Why:在Infopath生成的XML中,richtext的内容一般是这样表示的

<div xmlns="http://www.w3.org/1999/xhtml">this is some text</div>
<div xmlns="http://www.w3.org/1999/xhtml"> some other text;</div>
<div xmlns="http://www.w3.org/1999/xhtml"> aabbccddef</div><font color="#ff0000">red text:</font>normal text</div></div>

我们可以看到,Infopath并没有使用encoding,这里无论我们把<xsl:value-of>的disable-output-escaping属性的值赋为yes或者是no,都没办法得到正确的结果。得到的都是滤掉tag后没有格式的文字。

解决:用<xsl:copy-of>代替<xsl:value-of>。<xsl:copy-of>会把整个node tree拷贝出来,因此在没有encoding的时候可以不会丢失文字的格式属性。

 

2、无法用format-number函数格式化用科学技术法表示的数字

问题:infopath中由于表示精度的限制,decimal(double)的数据有时会被使用科学技术法保存,在试图通过xslt转换输出为html时使用format-number函数格式化会得到NaN

Why:xslt 1.0无法自动把科学技术法表示的数字转换为decimal(double)类型,用format-number函数试图转换时会被当作一般的string而得到NaN的输出

解决:写一个xslt专门转换科学技术法表示的数字

 转换的xslt程序

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="scient2Num">
<xsl:param name="pS"/>
<xsl:variable name="e" select="'E'"/>
<xsl:variable name="vNum1" select="substring-before($pS, $e)"/>
<xsl:if test="contains($pS,$e)">
<xsl:variable name="vZeroes" select="'0000000000000000000000000'"/>
<xsl:variable name="vOffset">
<xsl:choose>
<xsl:when test="substring(substring-after($pS, $e), 1, 1) = '+'">
<xsl:value-of select="substring(substring-after($pS, $e), 2)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="-1 * substring(substring-after($pS, $e), 2)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="vNum2">
<xsl:choose>
<xsl:when test="$vOffset >= 0">
<xsl:value-of
select="concat('1', substring($vZeroes, 1, $vOffset))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of
select="concat('0.', substring($vZeroes, 1, -$vOffset - 1), '1')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$vNum1 * $vNum2"/>
</xsl:if>
<xsl:if test = "contains($pS,$e) = false">
<xsl:value-of select="$pS"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

要使用上面的xslt,我们可以<xsl:include>把这段xslt代码包含到你的xslt中

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my=http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-02-10T02:54:16>

<xsl:include href="[your convert scientist number to decimal.xsl]"/>
<xsl:template match="/">
<html>
<body>
<xsl:variable name="n"> <xsl:call-template name="scient2Num">
<xsl:with-param name="pS" select="'6.11702127659574E-02'"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="format-number($n,'#.##%')"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

 

Happy coding!

Crazy

posted on 2006-08-29 15:03  CrazyCoder  阅读(1551)  评论(4编辑  收藏  举报