Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xslt

xpath

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?

like image 735
Muhammad Hewedy Avatar asked May 22 '26 04:05

Muhammad Hewedy


2 Answers

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>
like image 191
intgr Avatar answered May 24 '26 02:05

intgr


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
like image 26
Rubens Farias Avatar answered May 24 '26 02:05

Rubens Farias