Re: [xsl] Variable in XPath

Subject: Re: [xsl] Variable in XPath
From: Florent Georges <darkman_spam@xxxxxxxx>
Date: Wed, 27 Jun 2007 19:27:47 +0200 (CEST)
Garvin Riensche wrote:

  Hi

> <xsl:when test="doc('factbase.xml')/facts/class[@id eq $id
> and @owner eq $owner and @name eq $name]">

> This will return false because $id is empty. My quesion
> is, is there a default value that I can put in the select
> of <xsl:variable name="id" select"?????"/> so that the
> test above will return true?

  I think that if you can use XSLT 2.0, the more straight
forward would be to defined your predicates as functions:

    <xsl:function name="my:equals" as="xs:boolean">
      <xsl:param name="lhs" as="attribute()"/>
      <xsl:param name="rhs" as="item()?"/>
      <xsl:sequence select="
          if ( empty($rhs) ) then
            true()
          else
            $lhs eq $rhs"/>
    </xsl:function>

    <xsl:function name="my:matches" as="xs:boolean">
      <xsl:param name="elem"  as="element()"/>
      <xsl:param name="id"    as="item()?"/>
      <xsl:param name="owner" as="item()?"/>
      <xsl:param name="name"  as="item()?"/>
      <xsl:sequence select="
          my:equals($elem/@id, $id)
            and my:equals($elem/@owner, $owner)
            and my:equals($elem/@name, $name)"/>
    </xsl:function>

and then in your code:

    <xsl:if test="my:matches(the-elem, 'id', 'owner', 'name')">

  If the values to test against are global parameters, just
use:

    <xsl:function name="my:matches" as="xs:boolean">
      <xsl:param name="elem" as="element()"/>
      <xsl:sequence select="
          my:equals($elem/@id, $id)
            and my:equals($elem/@owner, $owner)
            and my:equals($elem/@name, $name)"/>
    </xsl:function>

and:

    <xsl:if test="my:matches(the-elem)">

  Regards,

--drkm





















	

	
		
___________________________________________________________________________ 
Dicouvrez une nouvelle fagon d'obtenir des riponses ` toutes vos questions ! 
Profitez des connaissances, des opinions et des expiriences des internautes sur Yahoo! Questions/Riponses 
http://fr.answers.yahoo.com

Current Thread