views:

563

answers:

1

At work I've been given the fun task of generating PDFs with XSL. The XML structure I'm working with is similar to

<records>
    <topLevel>
        <topLevelID></topLevelID>
        <secondLevel>
            <secondLevelID></secondLevelID>
            <thirdLevel>
            </thirdLevel>
            <thirdLevel>
            </thirdLevel>
        </secondLevel>
    </topLevel>
    <topLevel>
        <topLevelID></topLevelID>
        <secondLevel>
            <secondLevelID></secondLevelID>
            <thirdLevel>
            </thirdLevel>
            <thirdLevel>
            </thirdLevel>
        </secondLevel>
    </topLevel>
</records>

I would try to give a more meaningful example of the XML, but I don't feel like approaching any legal boundaries that may exist. With that XML structure, I have to output a block of text in the PDF for every thirdLevel node. The XSL I have so far is like

<xsl:for-each select ="topLevel">          
    <xsl:variable name="topID" select="topLevelID"/>
    <xsl:for-each select ="secondLevel">
        <xsl:variable name="secondID" select="secondLevelID"/>
        <xsl:for-each select="thirdLevel">            
            <fo:block-container position="absolute" height="12.8pt" width="220.8pt" left="160pt" display-align="auto">
                <xsl:attribute name="top">
                    <xsl:value-of select="concat(193 + [whatshouldgohere]), 'pt')"/>
                </xsl:attribute>
                <fo:block font-size="7pt">                          
                    <xsl:call-template name="insertThirdLevel"/>
                </fo:block>
            </fo:block-container>
        </xsl:for-each>
    </xsl:for-each>
</xsl:for-each>

Basically, I need to add some value to the top attribute to make the text for each thirdLevel node appear on its own line. I've tried using combinations of adding/multiplying by the ID (starts at 1 and increases by 1 for each set) and position(), but I can't seem to get it right.

+2  A: 

I think you should really look into <xsl:apply-templates>, it can save you a lot of typing.

Simplified version:

<xsl:variable name="line-height" select="10" />

<xsl:template match="/records">
  <xsl:apply-templates select="//thirdLevel" />
</xsl:template>

<xsl:template match="thirdLevel">
  <xsl:variable name="top" select="193 + position() * $line-height" />
  <fo:block-container top="{concat($top , 'pt')}">
    <fo:block font-size="7pt">                          
      <xsl:call-template name="insertThirdLevel"/>
    </fo:block>
  </fo:block-container>
</xsl:template>

<xsl:template name="insertThirdLevel">
  Third Level!
</xsl:template>

Simplified output ("fo" namespace excluded):

<fo:block-container top="203pt">
  <fo:block font-size="7pt">
    Third Level!
  </fo:block>
</fo:block-container>
<fo:block-container top="213pt">
  <fo:block font-size="7pt">
    Third Level!
  </fo:block>
</fo:block-container>
<fo:block-container top="223pt">
  <fo:block font-size="7pt">
    Third Level!
  </fo:block>
</fo:block-container>
<fo:block-container top="233pt">
  <fo:block font-size="7pt">
    Third Level!
  </fo:block>
</fo:block-container>
Tomalak
Thanks! I was overthinking this a lot. Thanks for simplifying it for me.
bcasp
Now that was quick. You're welcome. :-D
Tomalak