RE: [xsl] Using saxon:assign and testing content of variable

Subject: RE: [xsl] Using saxon:assign and testing content of variable
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 21 Sep 2005 04:24:43 +0100
You really ought to try and avoid using saxon:assign. There must be a better

If saxon:assign has any content, then the resulting value will be a result
tree fragment (or temporary tree): that is, the variable will contain a
document node which may or may not have children. You probably want to test
whether it has children using test="$pageNumber/child::node()".

Setting a variable to an "empty" result tree fragment (a document node with
no children) is surprisingly difficult! It can be done using something like:

<xsl:variable name="var">

basically, any xsl:variable instruction that has content, but whose content
is a no-op. You could also use

<xsl:variable name="var">


<xsl:variable name="var">
  <xsl:if test="false()"/>

Michael Kay

> -----Original Message-----
> From: Dan Vint [mailto:dvint@xxxxxxxxx] 
> Sent: 20 September 2005 18:56
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Using saxon:assign and testing content of variable
> I'm still working on my Index. Now I'm trying to gather the 
> index terms up 
> and group them and then generate the page numbers.
> I'm using Saxon 6.5 and the group and item extensions to make 
> this process 
> reasonable. I've got the headings seeming to work properly up 
> to 3 levels 
> with see and see also support. Now I'm trying to gather up 
> the page numbers 
> with their markup and output them as a single statement once 
> all the page 
> numbers are gathered.
> The process I wrote myself into has me collecting the page 
> numbers in a 
> variable that I can assign to with another saxon function.  
> This was the 
> template I had written:
> <xsl:template name="page-sequence">
>       <xsl:param name="NumberPage" select="false()" />
>     <xsl:if test="$NumberPage">
>          <xsl:choose>
>                  <xsl:when test="$pageNumber=''">
>                          <saxon:assign name="pageNumber">
>                                <fo:leader leader-pattern="dots" 
> leader-length="1in" />
>                                <fo:basic-link 
> internal-destination="{generate-id()}">
>                                      <fo:page-number-citation 
> ref-id="{generate-id()}" />
>                                 </fo:basic-link>
>                           </saxon:assign>
>                  </xsl:when>
>                  <xsl:otherwise>
> <!-- similar stuff here for continuation -->
>                   </xsl:otherwise>
>               </xsl:choose>
>      </xsl:if>
> </xsl:template>
> As you see I'm assigning the <fo> markup to the variable and in the 
> otherwise, I was adding additional page numbers with a comma 
> separator.
> What I'm at a loss for is the proper test in the <choice> to 
> see if there 
> is content in $pageNumber.
> First what is the proper way to reset the variable to be empty and 
> hopefully no nodes in the node-set? Does a simple <saxon:assign 
> name="pageNumber"/> do it?
> What is the best test to detect content in $pageNumber? I 
> tried count() but 
> it always returns 1, and the test for the empty string is not 
> working either.
> thanks
> ..dan
> --------------------------------------------------------------
> -------------
> Danny Vint
> Specializing in Panoramic Images of California and the West
> voice: 510-522-4703

Current Thread