·  XSLT – Elements – <xsl:if >

 

- Info :

  - Use this elements to check:

    - existance or values of attributes of current element or any of it's subelements or any of it's ancestors.

    - existance or number of any of it's and it's ancestor's elements.

    - This element can be nested and this has role of logical and as demonstrated in below example.

 

- Example:

 

    test.xml

    <?xml version="1.0"?>

    <?xml-stylesheet type="text/xsl" href="test.xsl"?>

    

    <people>

    

      <student id="  3">

        <name  id="44" >Jack  </name>

        <name          >Junior</name>

        <age           > 23   </age>

      </student>

    

      <teacher></teacher>

    

    </people>

    

    test.xsl

    <?xml version="1.0" ?>

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    

      <xsl:template match="/people/student">

    

        <xsl:if test="age"          >Student has at least one age subelement.         </xsl:if> <br/>      

        <xsl:if test="age=' 23   '" >Student has age element with string ' 23   '.    </xsl:if> <br/>

        <xsl:if test="age=   23   " >Student has age element with number 23.          </xsl:if> <br/>

        <xsl:if test="../teacher"   >Parent element has at least 1 teacher element.   </xsl:if> <br/>      

        <xsl:if test="count(name)>1">Student has more then 1 name subelement.         </xsl:if> <br/>       

    

        <xsl:if test="@id"          >Student has ID parameter.                        </xsl:if> <br/>

        <xsl:if test="@id='  3'"    >Student has ID parameter with string '  3'.      </xsl:if> <br/>

        <xsl:if test="@id= 3   "    >Student has ID parameter with number 3.          </xsl:if> <br/>

        <xsl:if test="../@id"       >Parent element has attribute id.                 </xsl:if> <br/>         

        <xsl:if test="name/@id>'40'">Student has name subelement with id>40.          </xsl:if> <br/>     

    

        <xsl:if test="@id=4 or name">Student's id=4 or has at lest 1 name subelement. </xsl:if> <br/> 

    

        <xsl:if test="@id=3">

          <xsl:if test="name">

            Studen has attribute id=3 and at lest 1 name element.

          </xsl:if> <br/>        

        </xsl:if> <br/>

            

      </xsl:template>

      

    </xsl:stylesheet>

    

    result

    Student has ID parameter.
    Student has name element with id>40.
    Parent element has attribute id.
    Student has at least one name element.
    Parent element has at least 1 teacher element.
    Student has more then 1 name element.
    Student's id=4 or has at lest 1 name element.

    Studen has attribute id=4 or has at lest 1 name element.