0

Hi guys I want to implement a heading structure in my pdf. The different headers should get the value from the old one and add 2 mm to get a structure I use xslt 2.0 and antenna house

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>
bvb1909
  • 191
  • 2
  • 6
  • 19

2 Answers2

2

Assuming that at the point you invoke the attribute set, the context item is the element whose @distance attribute you want to modify, you can do

<xsl:attribute name="distance" select="f:add-mm-to-distance(@distance, 2)"/>

and then you can write a function like

<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= ...

Obviously the detail of the function depends on what you expect to find in the @distance attribute.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
2

There's many ways to do it, but the way that answers your question with the least damage to your sample code would be to leave it to the Antenna House formatter to add the lengths:

<!-- 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>

However, this is inefficient, since the formatter would have to evaluate the expression every time that it encounters the 'distance' attribute (which isn't defined in XSL 1.1, BTW) in the FO document.

You could instead do the calculation in your XSLT beforehand, e.g.:

<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>

Alternatively, it's not hard to write an XSLT function that sums lengths so you can put a calculated value in the FO document.

Tony Graham
  • 7,306
  • 13
  • 20