سؤال

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?

هل كانت مفيدة؟

المحلول

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').

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top