fo:Table 到 html table - xslt
fo:Table to html table - xslt
我正在尝试编写一个 xslt 来生成另一个 xslt,其目的是仅用 html 标记替换所有 xsl:fo..XSLT 1
我在 "xsl" 命名空间周围使用 CDATA 以避免 xslt 处理此类标记。我的范围仅处理 xsl:fo 指令并替换例如:
<fo:table table-layout="fixed" width="100%" font-size="10pt">
<fo:table-column column-width="proportional-column-width(0.65)"/>
<fo:table-column column-width="proportional-column-width(0.35)"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell padding-before="0.5cm"></fo:table-cell>
<fo:table-cell padding-before="0.5cm">
<fo:block>
y
<![CDATA[ --> this is treated as text so i can copy it with <xsl-valueof select="."/>??
<xsl:choose>
<xsl:when test="...xpath'">
<xsl:value-of select="..." />,
</xsl:when>
<xsl:otherwise>
at <xsl:value-of select=..." />,
</xsl:otherwise>
</xsl:choose>]]>
</fo:block>
<fo:block space-before="0.5cm" text-align="center">
x
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
我想要翻译 fo:table+fo:table-body with table tag, and fo:table-column with td width="..%", fo:table-row with tr.. Td width 不是那么容易检索,因为宽度 属性 属于 fo:table-column 和 fo:table-cell 处理标签。
我尝试在读取 table-cell 时循环 fo:table-column 我正在写 td 并使用先例标记获得的 属性 column-width 计算宽度fo:table-列:我在fo:table-单元格选择
中使用标签table-列(第一个循环)的位置()
例如,这是我的 xsl:fo(上述)的 xslt 翻译器:
<xsl:template name="fo-table">
<xsl:param name="font-size" />
<xsl:param name="width" />
<xsl:variable name="cols" select="count(fo:table-column)"/>
<xsl:if test="fo:table-column">
<xsl:variable name="effective-cols" select="count(fo:table-body/fo:table-row/fo:table-cell)"/>
<xsl:if test="$cols = $effective-cols">
<table>
<xsl:for-each select="fo:table-body/fo:table-row">
<tr>
<xsl:for-each select="parent::*/parent::*/fo:table-column">
<xsl:variable name="width-proportional">
<xsl:value-of select="@column-width"/>
</xsl:variable>
<td>
<xsl:attribute name="width">
<xsl:call-template name="getPercentWidth">
<xsl:with-param name="proportional-value-width"><xsl:value-of select="$width-proportional"/></xsl:with-param>
</xsl:call-template>
</xsl:attribute>
abc <xsl:variable name="vPosition"><xsl:value-of select="position()"/></xsl:variable>
<xsl:for-each select="parent::*/fo:table-body/fo:table-row/*[$vPosition]">
<xsl:value-of select="local-name()"/><xsl:text> #10;</xsl:text> <!-- debug-->
<xsl:choose>
<xsl:when test="fo:block">
<xsl:for-each select="fo:block">
<xsl:call-template name="fo-block-table">
<xsl:with-param name="text-align"><xsl:value-of select="@text-align"/></xsl:with-param>
<xsl:with-param name="space-before"><xsl:value-of select="@space-before"/></xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
empty cell
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template name="fo-block-table">
<xsl:param name="text-align" />
<xsl:param name="space-before" />
<xsl:choose>
<xsl:when test="$text-align">
<div>
<xsl:attribute name="text-align">
<xsl:value-of select="normalize-space($text-align)"/>
</xsl:attribute>
<xsl:apply-templates select="."/>
</div>
</xsl:when>
<xsl:otherwise>
<div>
<xsl:apply-templates select="."/>
</div>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="getPercentWidth">
<xsl:param name="proportional-value-width"/>
<xsl:variable name="width" select="normalize-space($proportional-value-width)"/>
<xsl:variable name="begin"> <xsl:value-of select="string-length(substring-before($width, '('))" /></xsl:variable>
<xsl:variable name="last"> <xsl:value-of select="string-length(substring-before($width,')'))" /></xsl:variable>
<xsl:variable name="val" select="fn:substring($width, $begin, $last)" />
<xsl:variable name="val1" select="substring-after($val,'(')"/>
<xsl:variable name="cent" select="100"/>
<xsl:value-of select="concat(($val1 * $cent),'%')"/>
</xsl:template>
但我无法理解为什么所有 td 都包含 'y',x 并且当它只属于空 table-cell 时为空,似乎它读取所有 fo:block..
<table>
<tr>
<td width="65%">
abc
table-cell #10;
empty cell
table-cell #10;<div>
y
</div>
<div text-align="center">
x
</div>
</td>
<td width="35%">
abc
table-cell #10;
empty cell
table-cell #10;<div>
y
</div>
<div text-align="center">
x
</div>
</td>
</tr>
</table>
我需要获得:
<table>
<tr>
<td width="65%">
abc
table-cell #10;
empty cell
</td>
<td width="35%">
abc
table-cell #10;
<div>
y
</div>
<div text-align="center">
x
</div>
</td>
</tr>
</table>
如果我替换第二个循环
xsl : 为每个
与
xsl : template
不匹配任何东西!
也许 *[$vPosition] 不起作用,但如果我将数字替换为 1 或 2..
怎么了?
多谢指教!
罗比
问题出在这一行...
<xsl:for-each select="parent::*/fo:table-body/fo:table-row/*[$vPosition]">
或者更确切地说,这是由于 vPosition
变量的定义方式:
<xsl:variable name="vPosition"><xsl:value-of select="position()"/></xsl:variable>
通过使用 xsl:value-of
,您实际上是在将 vPosition
设置为字符串值,而不是数值。当您在条件中使用字符串值时,例如 [*$vPosition]
如果该字符串不为空,它将始终 return 为真。
尝试将变量声明更改为此,这会将 vPosition
设置为数字
<xsl:variable name="vPosition" select="position()" />
我正在尝试编写一个 xslt 来生成另一个 xslt,其目的是仅用 html 标记替换所有 xsl:fo..XSLT 1
我在 "xsl" 命名空间周围使用 CDATA 以避免 xslt 处理此类标记。我的范围仅处理 xsl:fo 指令并替换例如:
<fo:table table-layout="fixed" width="100%" font-size="10pt">
<fo:table-column column-width="proportional-column-width(0.65)"/>
<fo:table-column column-width="proportional-column-width(0.35)"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell padding-before="0.5cm"></fo:table-cell>
<fo:table-cell padding-before="0.5cm">
<fo:block>
y
<![CDATA[ --> this is treated as text so i can copy it with <xsl-valueof select="."/>??
<xsl:choose>
<xsl:when test="...xpath'">
<xsl:value-of select="..." />,
</xsl:when>
<xsl:otherwise>
at <xsl:value-of select=..." />,
</xsl:otherwise>
</xsl:choose>]]>
</fo:block>
<fo:block space-before="0.5cm" text-align="center">
x
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
我想要翻译 fo:table+fo:table-body with table tag, and fo:table-column with td width="..%", fo:table-row with tr.. Td width 不是那么容易检索,因为宽度 属性 属于 fo:table-column 和 fo:table-cell 处理标签。
我尝试在读取 table-cell 时循环 fo:table-column 我正在写 td 并使用先例标记获得的 属性 column-width 计算宽度fo:table-列:我在fo:table-单元格选择
中使用标签table-列(第一个循环)的位置()例如,这是我的 xsl:fo(上述)的 xslt 翻译器:
<xsl:template name="fo-table">
<xsl:param name="font-size" />
<xsl:param name="width" />
<xsl:variable name="cols" select="count(fo:table-column)"/>
<xsl:if test="fo:table-column">
<xsl:variable name="effective-cols" select="count(fo:table-body/fo:table-row/fo:table-cell)"/>
<xsl:if test="$cols = $effective-cols">
<table>
<xsl:for-each select="fo:table-body/fo:table-row">
<tr>
<xsl:for-each select="parent::*/parent::*/fo:table-column">
<xsl:variable name="width-proportional">
<xsl:value-of select="@column-width"/>
</xsl:variable>
<td>
<xsl:attribute name="width">
<xsl:call-template name="getPercentWidth">
<xsl:with-param name="proportional-value-width"><xsl:value-of select="$width-proportional"/></xsl:with-param>
</xsl:call-template>
</xsl:attribute>
abc <xsl:variable name="vPosition"><xsl:value-of select="position()"/></xsl:variable>
<xsl:for-each select="parent::*/fo:table-body/fo:table-row/*[$vPosition]">
<xsl:value-of select="local-name()"/><xsl:text> #10;</xsl:text> <!-- debug-->
<xsl:choose>
<xsl:when test="fo:block">
<xsl:for-each select="fo:block">
<xsl:call-template name="fo-block-table">
<xsl:with-param name="text-align"><xsl:value-of select="@text-align"/></xsl:with-param>
<xsl:with-param name="space-before"><xsl:value-of select="@space-before"/></xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
empty cell
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template name="fo-block-table">
<xsl:param name="text-align" />
<xsl:param name="space-before" />
<xsl:choose>
<xsl:when test="$text-align">
<div>
<xsl:attribute name="text-align">
<xsl:value-of select="normalize-space($text-align)"/>
</xsl:attribute>
<xsl:apply-templates select="."/>
</div>
</xsl:when>
<xsl:otherwise>
<div>
<xsl:apply-templates select="."/>
</div>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="getPercentWidth">
<xsl:param name="proportional-value-width"/>
<xsl:variable name="width" select="normalize-space($proportional-value-width)"/>
<xsl:variable name="begin"> <xsl:value-of select="string-length(substring-before($width, '('))" /></xsl:variable>
<xsl:variable name="last"> <xsl:value-of select="string-length(substring-before($width,')'))" /></xsl:variable>
<xsl:variable name="val" select="fn:substring($width, $begin, $last)" />
<xsl:variable name="val1" select="substring-after($val,'(')"/>
<xsl:variable name="cent" select="100"/>
<xsl:value-of select="concat(($val1 * $cent),'%')"/>
</xsl:template>
但我无法理解为什么所有 td 都包含 'y',x 并且当它只属于空 table-cell 时为空,似乎它读取所有 fo:block..
<table>
<tr>
<td width="65%">
abc
table-cell #10;
empty cell
table-cell #10;<div>
y
</div>
<div text-align="center">
x
</div>
</td>
<td width="35%">
abc
table-cell #10;
empty cell
table-cell #10;<div>
y
</div>
<div text-align="center">
x
</div>
</td>
</tr>
</table>
我需要获得:
<table>
<tr>
<td width="65%">
abc
table-cell #10;
empty cell
</td>
<td width="35%">
abc
table-cell #10;
<div>
y
</div>
<div text-align="center">
x
</div>
</td>
</tr>
</table>
如果我替换第二个循环 xsl : 为每个 与
xsl : template
不匹配任何东西! 也许 *[$vPosition] 不起作用,但如果我将数字替换为 1 或 2..
怎么了?
多谢指教!
罗比
问题出在这一行...
<xsl:for-each select="parent::*/fo:table-body/fo:table-row/*[$vPosition]">
或者更确切地说,这是由于 vPosition
变量的定义方式:
<xsl:variable name="vPosition"><xsl:value-of select="position()"/></xsl:variable>
通过使用 xsl:value-of
,您实际上是在将 vPosition
设置为字符串值,而不是数值。当您在条件中使用字符串值时,例如 [*$vPosition]
如果该字符串不为空,它将始终 return 为真。
尝试将变量声明更改为此,这会将 vPosition
设置为数字
<xsl:variable name="vPosition" select="position()" />