tags:

views:

249

answers:

2

Hello,

I need to make an statement where the test pass if there is just one asterisk in a string from the source document.

Thus, something like

<xslt:if test="count(find('\*', @myAttribute)) = 1)>
    There is one asterisk in myAttribute
</xslt:if>

I need the functionality for XSLT 1, but answers for XSLT 2 will be appreciated as well, but won't get acceptance unless its impossible in XSLT 1.

+1  A: 

In XPath 1.0, we can do it by removing all asterisks using translate and comparing the length:

string-length(@myAttribute) - string-length(translate(@myAttribute, '*', '')) = 1

In XPath 2.0, I'd probably do this:

count(string-to-codepoints(@myAttribute)[. = string-to-codepoints('*')]) = 1
Pavel Minaev
+1  A: 

Another solution that should work in XPath 1.0:

contains(@myAttribute, '*') and not(contains(substring-after(@myAttribute, '*'), '*'))
Jörn Horstmann
Nice answer. It looks a bit more specific than the question asks, but actually works well in my case, since the asterisk would always end the phrase.thanks, but the xpath 2 answer is slightly better.
Hugo