【问题标题】:Get the value from a attribute set attribute从属性集属性中获取值
【发布时间】:2015-09-10 10:31:05
【问题描述】:
大家好,我想在我的 pdf 中实现一个标题结构。不同的标题应该从旧标题中获取值并添加 2 mm 以获得结构
我使用 xslt 2.0 和天线屋
xslt:
<xsl:attribute-set name="head5">
<xsl:attribute
name="distance">2cm</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="head6">
<xsl:attribute
name="distance"><xsl:value-of select=""/>
</xsl:attribute>
</xsl:attribute-set>
【问题讨论】:
标签:
xml
xslt
xslt-2.0
xsl-fo
【解决方案1】:
假设在你调用属性集的时候,上下文项是你要修改@distance属性的元素,你可以这样做
<xsl:attribute name="distance" select="f:add-mm-to-distance(@distance, 2)"/>
然后你可以写一个类似的函数
<xsl:function name="f:add-mm-to-distance" as="xs:string">
<xsl:param name="in" as="xs:string"/>
<xsl:param name="add" as="xs:integer"/>
<xsl:choose>
<xsl:when test="ends-with($in, 'mm')">
<xsl:sequence select="concat(number(substring-before($in, 'mm'))+$add, 'mm')"/>
</xsl:when>
<xsl:when test= ...
显然,函数的细节取决于您期望在@distance 属性中找到的内容。
【解决方案2】:
有很多方法可以做到这一点,但以对示例代码损害最小的方式回答您的问题的方法是将其留给 Antenna House 格式化程序以添加长度:
<!-- Untested. -->
<xsl:attribute-set name="head5">
<xsl:attribute
name="distance">2cm</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="head6">
<xsl:attribute name="distance">
<xsl:variable name="dummy" as="element()">
<dummy xsl:use-attribute-sets="head5" />
</xsl:variable>
<xsl:value-of select="$dummy/@distance"/> + 2mm</xsl:attribute>
</xsl:attribute-set>
但是,这是低效的,因为格式化程序每次遇到 FO 文档中的“距离”属性(顺便说一句,XSL 1.1 中没有定义)时都必须对表达式求值。
您可以改为预先在 XSLT 中进行计算,例如:
<xsl:variable name="distances"
select="'2cm', '22mm'"
as="xs:string+" />
<xsl:attribute-set name="head5">
<xsl:attribute name="distance" select="$distances[1]" />
</xsl:attribute-set>
<xsl:attribute-set name="head6">
<xsl:attribute name="distance" select="$distances[2]" />
</xsl:attribute-set>
另外,编写一个对长度求和的 XSLT 函数并不难,因此您可以将计算值放入 FO 文档中。