This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: "remembering" variable values


> [snip]... Hence I thought that one way could be that instead of having 
> "Figure 1"  as 
> the text of <figure>, I have only "Figure" and I somehow 
> generate the figure number using an XPath expression.

> Thus the new content of figure is:
> <figure>
> Figure
> </figure>
> 
> My XSL code is as follows:
> 
> <xsl:template match="figure">
> <!--Creating a variable and assigning a value to it-->
> <xsl:variable name="fig" select="count(preceding::figure) + 1"/>

Fine so far; though you could also use
<xsl:variable name="fig"><xsl:number level="any"/></xsl:variable> which some
processors might execute faster.

> Now, my problems are as follows:
> 
> 1.) The above method, in a way, automates the process. But there is a flaw

> in this. Suppose that a particular figure is referenced more than once in 
> the document. e.g. lets say that I want to refer to "Figure 1" again,
after 
> say "Figure 2". ... Is there any way once a generate a particular number 
> for a figure, I somehow establish a relation between the gif file that it 
> points to and the number so that if the code comes across the same 
> attribute value (i.e. the gif file) again in the document, it generates
the 
> corresponding number for that, instead of generating a new number?

What you are saying is, when you encounter <fig ref="xyz.gif"/>, you want to
output the number of the first figure having that ref value; and for good
measure, you don't want to increment the figure number for this one.

You can achieve the first part by writing
<xsl:variable name="fig">
<xsl:for-each select="//figure[@ref=current()/@ref][1]">
<xsl:number level="any"/>
</xsl:for-each>
</xsl:variable>

(The xsl:for-each here is not to process a set of things, it is to change
the current node, because xsl:number only works on the current node)

And you can achieve the second part by changing the xsl:number to

<xsl:number level="any" count="figure[not(@ref=preceding::figure/@ref)]"/>

I have to admit, though, that the performance of this is likely to be
horrendous. For each figure, it is likely to look for each of the preceding
figures, and for each preceding figure, it will look to see if there is a
previous one with the same ref attribute. That's n-cubed performance. If you
want to improve this, the way forward is probably to define an xsl:key, but
I won't explore that.
> 
> 2.) ... However, how do I append this 
> element to the gif file and subsequently display the combined result (the 
> gif file and the <caption>) in another browser window?
> 
To do this you need to generate multiple output files. This extension is
available in many of the currently-available XSLT processors.

Mike Kay


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]