search.xslt

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
		xmlns:html="http://www.w3.org/1999/xhtml"
		xmlns:xlink="http://www.w3.org/1999/xlink"
		version="1.0"
>
  <xsl:output doctype-system="cexml_message.dtd"
	      indent="yes"
	      />

  <xsl:template match="/">
    <xsl:apply-templates select="cexml"/>
  </xsl:template>
	
  <xsl:template match="cexml">
    <xsl:element name="cexml">
      <!-- search for every builtobject with a builtobject parent
      (excluding the builtobjects which are characteristics), with the
      extra requirement that they have the attribute mode="search" --> 
      <xsl:apply-templates
      select='//builtobject/builtobject[@mode="search"]'  mode="search" />
      <xsl:apply-templates select="quantification" />
      <xsl:apply-templates select="function" />
      <xsl:apply-templates select="unit" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="builtobject">
    <xsl:element name="builtobject">
      <xsl:call-template name="copy-attributes"/>
      <xsl:apply-templates select="name" />
      <xsl:apply-templates select="characteristics" />
      <xsl:apply-templates select="builtobject" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="characteristics">
    <xsl:element name="characteristics">
      <xsl:call-template name="copy-attributes"/>
      <xsl:apply-templates select="builtobject" />
      <xsl:apply-templates select="amount" />
      <xsl:apply-templates select="quantification" />
      <xsl:apply-templates select="function" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="name">
    <xsl:element name="name">
      <xsl:call-template name="copy-attributes"/>
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="amount">
    <xsl:element name="amount">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="quantification">
    <xsl:element name="quantification">
      <xsl:call-template name="copy-attributes"/>
      <xsl:apply-templates select="name" />
      <xsl:apply-templates select="amount" />
      <xsl:apply-templates select="unit" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="function">
    <xsl:element name="function">
      <xsl:call-template name="copy-attributes"/>
      <xsl:apply-templates select="name" />
      <xsl:apply-templates select="quantification" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="unit">
    <xsl:element name="unit">
      <xsl:call-template name="copy-attributes"/>
      <xsl:apply-templates select="name" />
    </xsl:element>
  </xsl:template>


  <xsl:template name="copy-attributes"> 
    <xsl:for-each select="@*">
      <xsl:copy />
    </xsl:for-each>
  </xsl:template>

<!-- TEMPLATES SPECIFIC TO THIS STYLESHEET BELOW THIS LINE -->

  <xsl:template match="builtobject" mode="search">
    <!-- I'm a barbarian, but I'm only implementing the search for
    "span" (or "overspanning" in Dutch) at the moment.
    So: search for
    * a builtobject with the correct name
    * with the correct value for quantity "span"
    -->
    <xsl:param name="current_name">
      <xsl:value-of select="./name"/>
    </xsl:param>
    <xsl:param name="current_amount">
      <xsl:value-of
      select='./characteristics/quantification[name="span"]/amount'/>  
    </xsl:param>
    <xsl:for-each
    select='//builtobject/builtobject[name=$current_name][@mode!="search"]'>
      <xsl:if
      test='characteristics/quantification[name="span"]/amount=$current_amount'> 
	<xsl:element name="builtobject">
	  <xsl:call-template name="copy-attributes"/>
	  <xsl:attribute name="mode">found</xsl:attribute>
	  <xsl:apply-templates select="name" />
	  <xsl:apply-templates select="characteristics" />
	</xsl:element>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>