select with if then else : select « XSLT stylesheet « XML






select with if then else


File: Data.xml
<?xml version="1.0"?>
<countries>
  <country name="France" />
  <country name="Germany" />
  <country name="Israel" />
  <country name="Japan" />
  <country name="Poland" />
  <country name="United States" selected="yes" />
  <country name="Venezuela" />
</countries>

File: Transform.xslt

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

  <xsl:param name="schema-version" select="4.0" />

  <xsl:template match="/">
    <promotion>
      <xsl:variable name="attname"
        select="if ($schema-version lt 3.0) 
               then 'code' 
               else 'reason-code'" />
      <xsl:attribute name="{$attname}" select="17" />
    </promotion>

  </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?><promotion reason-code="17"/>

 








Related examples in the same category

1.Parent and attribute
2.select="../@attribute"
3.child
4.Get value from tag with {}
5.Node selection by level
6.Select Node by index
7.Select attribute value and output to a list
8.context position and context size
9.select="document('')/*/book:category[@code=current()/@category]/@desc"
10.select distinct values
11.Select one from the target value list
12.select="@*" (at)
13.Select one tag from a list of tags
14.select="employees/employee[2]/following::contact/name/firstName"
15.select="employees/employee[2]/preceding::contact/name/firstName"
16.select="employee[@dept='programming']"
17.select="employees/head:header/namespace::head"
18.value-of select="person[position()=3]/name"