2011-05-21 172 views
5

我正在创建xsl-fo到rtf样式表。我遇到的一个问题是将xsl-fo文档中的多个度量单位转换为缇(rtf度量单位)。xsl转换/翻译模板

一个特定的代码片caluclates的列的宽度:

<xsl:value-of select="sum(preceding-sibling: 
    :fo:table-column/@column-width) + @column-width"/> 

...问题是的/@column-width的值可以是从1in(1英寸)什么20px(20个像素),因此,当我做它会失败的总和。

我需要以某种方式转换@column-width以缇equivelant: 1pt = 19.95 twips, 1px = 15 twips, 1pc = 240 twips, 1in = 1440 twips, 1cm = 567 twips, 1mm = 56.7 twips, 1em = 240 twips

我大概可以写,可以做转换的方法,但我相信有一些方法来利用translate()功能做这更有效。

请大家注意,我的XSL是不是所有的大,所以如何实现这样的一个例子可以理解

编辑

我设法找到了我想要的东西,但不知道如何调用从上述计算这个模板:

<xsl:template match="@*" mode="convert-to-twips"> 
    <xsl:variable name="scaling-factor"> 
     <xsl:choose> 
     <xsl:when test="contains (., 'pt')">19.95</xsl:when> 
     <xsl:when test="contains (., 'px')">15</xsl:when> 
     <xsl:when test="contains (., 'pc')">240</xsl:when> 
     <xsl:when test="contains (., 'in')">1440</xsl:when> 
     <xsl:when test="contains (., 'cm')">567</xsl:when> 
     <xsl:when test="contains (., 'mm')">56.7</xsl:when> 
     <xsl:when test="contains (., 'em')">240</xsl:when> 
     <!-- guess: 1em = 12pt --> 
     <xsl:otherwise>1</xsl:otherwise> 
     </xsl:choose> 
    </xsl:variable> 

    <xsl:variable name="numeric-value" 
     select="translate (., '-.ptxcinme', '-.')"/> 
    <xsl:value-of select="$numeric-value * $scaling-factor"/> 

</xsl:template> 
+0

好问题,+1。查看我的答案,获得完整而简单的解决方案。 – 2011-05-21 22:11:44

+0

是的,这是一个*完整的解决方案 - 不仅仅是伪代码。 – 2011-05-22 00:09:15

+0

我的答案只是一个例子,说明如何使用模板规则来使用'xsl:call-template'。你如何使用'translate'也有一点小错误。看到我的答案,希望它有帮助。 – 2011-05-22 09:28:10

回答

3

这种转变

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ext="http://exslt.org/common" 
xmlns:fo="some:fo" xmlns:my="my:my" > 
<xsl:output method="text"/> 

<my:units> 
    <unit name="pt">19.95</unit> 
    <unit name="in">1440</unit> 
    <unit name="cm">567</unit> 
    <unit name="mm">56.7</unit> 
    <unit name="em">240</unit> 
    <unit name="px">15</unit> 
    <unit name="pc">240</unit> 
</my:units> 

<xsl:variable name="vUnits" select= 
     "document('')/*/my:units/*"/> 

<xsl:template match="/"> 
    <xsl:apply-templates select="*/*/*/@column-width"/> 
</xsl:template> 

<xsl:template match="@column-width"> 
    <xsl:variable name="vQuantity" select= 
     "substring(.,1, string-length() -2)"/> 
    <xsl:variable name="vUnit" select= 
     "substring(., string-length() -1)"/> 

    <xsl:variable name="vrtfAccumWidth"> 
    <num>0</num> 
    <xsl:for-each select= 
    "../preceding-sibling::fo:table-column/@column-width"> 
    <xsl:variable name="vQ" select= 
     "substring(.,1, string-length() -2)"/> 
    <xsl:variable name="vU" select= 
     "substring(., string-length() -1)"/> 

    <num> 
     <xsl:value-of select= 
     "$vQ * $vUnits[@name=$vU]"/> 
    </num> 
    </xsl:for-each> 
    </xsl:variable> 

    <xsl:value-of select= 
    "$vQuantity * $vUnits[@name=$vUnit] 
    + 
    sum(ext:node-set($vrtfAccumWidth)/num) 
    "/> 

    <xsl:text>&#xA;</xsl:text> 
</xsl:template> 
</xsl:stylesheet> 

当下面的XML文档应用(如没有被提供!):

<fo:fo xmlns:fo="some:fo"> 
<fo:table> 
    <fo:table-column column-width="2pt"/> 
    <fo:table-column column-width="2in"/> 
    <fo:table-column column-width="2cm"/> 
    <fo:table-column column-width="2mm"/> 
    <fo:table-column column-width="2em"/> 
    <fo:table-column column-width="2px"/> 
    <fo:table-column column-width="2pc"/> 
</fo:table> 
</fo:fo> 

产生想要的,正确的结果

39.9 
2919.9 
4053.9 
4167.3 
4647.3 
4677.3 
5157.3 

注意:如果对fo:table-column\@column-width的大序列需要更高效的解决方案,那么FXSL - scanl可以使用模板/函数 - 请参阅我对上一个问题的回答以获得完整的代码示例。

+0

@ Flynn1179:我的算术*是*正确的,有时可能看起来很容易可能“容易”变成不正确:) – 2011-05-21 23:02:24

+0

是的,我已经注意到了,并删除了我的评论;我误解了问题的重点在于计算列的宽度,认为它不应该是累积的。 – Flynn1179 2011-05-21 23:29:31

+0

@ Flynn1179:没问题,交配。我们都是人类,至少有时候应该会犯错误:) – 2011-05-22 00:09:56

1

你可以使用这个模板来利用现有的一个你有:

<xsl:template match="@column-width"> 
    <xsl:variable name="previous"> 
    0<xsl:apply-templates select="../preceding-sibling::*[1]/@column-width" /> 
    </xsl:variable> 
    <xsl:variable name="this"> 
    <xsl:apply-templates select="." mode="convert-to-twips"/> 
    </xsl:variable> 
    <xsl:value-of select="$previous + $this" /> 
</xsl:template> 

这是相当简单的,你可以看到,简单地计算前一列的宽度,然后将其添加到当前的一个。您可能会注意到第一个变量的<xsl:apply-templates指令前面有一个0;这是为了确保给变量赋予一个有效的数字。如果没有以前的列,则它将存储'0'而不是''。

严格地说,您可以包含现有模板的正文替代第二个变量,并且在底部有<xsl:value-of select="$previous + ($numeric-value * $scaling-factor)" />;这完全取决于你。

1

你也可以使用模板功能和xsl:call-template。窃取@Dimitre输入示例(不要恨我:)我向您展示如何使用您的转换模板规则与xsl:call-template

在变换中,我遍历每个table-column,从而收集转换的值。要转换该值,我只是调用您的原始模板规则(稍微调整)。然后我使用sum来执行这些值的总和。

请注意,如果您没有将由translate返回的值强制转换为数字,则符合XSLT 2.0的处理器将返回运行时错误。


XSLT 2.0撒克逊-B 9.0.0.4J测试

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="some:fo"> 
    <xsl:output method="text"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="fo:table/fo:table-column"> 

     <xsl:variable name="twips"> 
      <twips> 
       <xsl:for-each select="preceding-sibling::fo:table-column/@column-width"> 
        <twip> 
         <xsl:call-template name="convert-to-twips"> 
          <xsl:with-param name="value" select="."/> 
         </xsl:call-template> 
        </twip> 
       </xsl:for-each> 
       <twip> 
         <xsl:call-template name="convert-to-twips"> 
          <xsl:with-param name="value" select="@column-width"/> 
         </xsl:call-template> 
       </twip> 
      </twips> 
     </xsl:variable> 

     <xsl:value-of select="sum($twips//twip)"/><xsl:text> </xsl:text> 

    </xsl:template> 

    <xsl:template name="convert-to-twips"> 

     <xsl:param name="value"/> 

     <xsl:variable name="scaling-factor"> 
      <xsl:choose> 
       <xsl:when test="contains ($value, 'pt')">19.95</xsl:when> 
       <xsl:when test="contains ($value, 'px')">15</xsl:when> 
       <xsl:when test="contains ($value, 'pc')">240</xsl:when> 
       <xsl:when test="contains ($value, 'in')">1440</xsl:when> 
       <xsl:when test="contains ($value, 'cm')">567</xsl:when> 
       <xsl:when test="contains ($value, 'mm')">56.7</xsl:when> 
       <xsl:when test="contains ($value, 'em')">240</xsl:when> 
       <!-- guess: 1em = 12pt --> 
       <xsl:otherwise>1</xsl:otherwise> 
      </xsl:choose> 
     </xsl:variable> 

     <xsl:variable name="numeric-value" 
      select="number(translate ($value, '-.ptxcinme', '-.'))"/> 
     <xsl:value-of select="$numeric-value * $scaling-factor"/> 

    </xsl:template> 

</xsl:stylesheet> 

这种变换施加在输入:

<fo:fo xmlns:fo="some:fo"> 
<fo:table> 
    <fo:table-column column-width="2pt"/> 
    <fo:table-column column-width="2in"/> 
    <fo:table-column column-width="2cm"/> 
    <fo:table-column column-width="2mm"/> 
    <fo:table-column column-width="2em"/> 
    <fo:table-column column-width="2px"/> 
    <fo:table-column column-width="2pc"/> 
</fo:table> 
</fo:fo> 

产地:

39.9 2919.9 4053.9 4167.3 4647.3 4677.3 5157.3