(Q23) In the document "company.xml", find all the elements that are reachable from the employee with serial number 12345 by child or reference connections.

FUNCTION connected(ELEMENT $e) RETURNS LIST(ELEMENT)
{ 
    $e/* UNION $e/@*->* 
}

FUNCTION reachable(ELEMENT $e) RETURNS LIST(ELEMENT)
{ 
    $e UNION reachable(connected($e)) 
}

reachable(document("company.xml")/emp[serial="12345"])

XSLT equivalent to (Q23)

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:variable name="e" select="//emp[serial='12345']"/>
    <xsl:apply-templates select="$e/* | id($e/@*)"/>
  </xsl:template>
  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates select="* | id(@*)"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="text()"/>
</xsl:transform>
  • The XSLT solution, while it could have been implemented using a recursive named template, uses template rules in order to highlight the convenience afforded by template rules.
<<<  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15    >>>