Use if statement to test if an element as an attribute : if « XSLT stylesheet « XML Tutorial






File: Data.xml

<?xml version="1.0" standalone="yes"?>
<poem author="jm" year="1667">
  <verse>line 1</verse>
  <verse>line 2</verse>
  <verse>line 3</verse>
  <verse>line 4</verse>
</poem>

File: Transform.xslt
<?xml version="1.0" standalone="yes"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:template match="poem">

    <xsl:if test="@year">
      The poem has a flavor attribute.
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Output:


      The poem has a flavor attribute.








5.44.if
5.44.1.The Element: Conditional Processing
5.44.2.Use if statement to test if an element as an attribute
5.44.3.Check two attributes
5.44.4.Nested if statement
5.44.5.if test="not(preceding-sibling::address[zip=$lastZip])"
5.44.6.if test="(position() mod 2) = 1"
5.44.7.select with if them else statement
5.44.8.xsl:if instruction enables conditional processing
5.44.9.Use if statement to check whether it is the last