Muhammad Hewedy
Muhammad Hewedy

Reputation: 30058

What does test="element[@attribute]" do in XSLT?

What does this do exactly?

<xsl:if test="./employee/first-name[@id='14']" >

I mean, when will this case be true, if ./employee/first-name[@id] != null or "", or what?

Edit

I have edited the statement above, so it tests if element first-name with id=14 have a body or its body contains a data or return true if first-name event don't have a body?

Upvotes: 2

Views: 10615

Answers (3)

intgr
intgr

Reputation: 20466

If the query in <xsl:if test=...> returns at least one node then the conditional evaulates to true.

The new XSLT expression you gave:

<xsl:if test="./employee/first-name[@id='14']" >

will evaluate to true if and only if there exists a first-name node under employee whose id attribute is equal to the string '14'

<employee><first-name id="" /></employee>     <!-- does NOT match -->
<employee><first-name id="014" /></employee>  <!-- does NOT match -->
<employee>
  <first-name id="foobar" />
  <first-name id="14" />                      <!-- DOES match -->
</employee>

Upvotes: 11

Tomalak
Tomalak

Reputation: 338208

<xsl:if> tests an expression (like the if in traditional languages). The result of the expression is evaluated as a Boolean.

In your case, the expression is an XPath selector (/employee/first-name[@id]) - it will always return a node-set. Node-sets are evaluated as false only when they are empty (e.g. no matching nodes found).

The expression could also be something like number(/employee/id). Now the expression result is a number, which is evaluated as false if it is zero or NaN, true in all other cases.

Other values that are false are the empty string '' (true in all other cases) and the result of false() itself. Note that there also is a true() function to return a Boolean true.

Also note that the string 'false' evaluates to true, per the the described rule for strings.

Upvotes: 2

Rubens Farias
Rubens Farias

Reputation: 57946

Seems there's some missing pieces, as last or "" will throw an error. But, let's see:

    ./                    search within current node
    employee/first-name   for an employee tag with an first-name child tag
    [@id]                 where first-name tag have an id attribute
    !=null                and that first-name tag have some value

Upvotes: 3

Related Questions