“If” Example Answers

Consider this fragment:

<r:recipe>
  <dc:title>Recipe Title</dc:title>
  <r:name>Recipe Name</r:name>
  ...
</r:recipe>

How are these three different?

<xsl:variable name="title" select="(r:name|dc:title)[1]"/>

Returns “dc:title”.

<xsl:variable name="title">
  <xsl:choose>
    <xsl:when test="r:name">
      <xsl:copy-of select="r:name"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:copy-of select="dc:title"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

Returns a copy ofr:name”.

<xsl:variable name="title"
              select="if (r:name) then r:name
              else dc:title"/>

Returns “r:name”.