1

In my transformation there is an expression some elements are repeatedly tested against. To reduce redundancy I'd like to encapsulate this in an xsl:key like this (not working):

<xsl:key name="td-is-empty" match="td" use="not(./node()[normalize-space(.) or ./node()])" />

The expected behaviour is the key to yield a boolean value of true in case the expression is evaluated successfully and otherwise false. Then I'd like to use it as follows:

<xsl:template match="td[not(key('td-is-empty', .))]" />

Is this possible and in case yes, how?

hielsnoppe
  • 2,819
  • 3
  • 31
  • 56

1 Answers1

1

I think with XSLT 1.0 a key value is always of type string so in your sample the key value with either be the string true or the string false. You could then call key('td-is-empty', 'true') to find all td element nodes for which the expression is true and key('td-is-empty', 'false') to find all td elements for which the expression is false.

You seem to want to do something differently with your key however, like storing the result of the use expression for each td node, based on node identity. I don't think that is how keys work in XSLT.

[edit] You might however be able to express your requirement as

<xsl:template match="td[count(. | key('td-is-empty', 'false')) = count(key('td-is-empty', 'false'))]">...</xsl:template>

That matches those td elements which are a member of the set of elements found by key('td-is-empty', 'false').

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thank you! Seems like I had some misunderstanding on how keys work. I will try your suggestion, but probably it won't simplify my code help unless the condition becomes more complex – hielsnoppe Dec 21 '12 at 09:18