Re: [xsl] How can I avaliate an expression XPath in a variable

Subject: Re: [xsl] How can I avaliate an expression XPath in a variable
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 23 Oct 2002 11:25:10 +0100
Hi Giovani,

> I've a problem when I try to avaliate the value of a variable that
> contain an expression XPath, as below:
> The XML file:
> <?xml version="1.0" encoding="UTF-8"?>
> <di>
>   <nome>Departamento de Informatica</nome>
>   <docentes>
>     <prof>Pedro Silva</prof>
>     <prof>Jorge Santos</prof>
>     <prof>Paulo Camargo</prof>
>   </docentes>
> </di>
> The stylesheet:
> <xsl:for-each select="/di/docentes/prof">
>   <xsl:element name="profRef">
>     <xsl:attribute name="href">
>       <xsl:variable name="pos" select="concat('/di/docentes/prof', '[',
> position(), ']')"/>
>       #<xsl:value-of select="translate($pos, ' ', '-')"/>
>     </xsl:attribute>
>   </xsl:element>
> </xsl:for-each>

You're making things far more complicated than they need to be. Within
the xsl:for-each, the current node is the prof element that you're
currently processing. To get the value of that prof element, all you
have to do is use the path:


So try:
  <xsl:for-each select="/di/docentes/prof">
    <xsl:element name="profRef">
      <xsl:attribute name="href">
        <xsl:text />#<xsl:value-of select="translate(., ' ', '-')" />

In fact, here you can make things even simpler by using an attribute
value template for the href attribute:

  <xsl:for-each select="/di/docentes/prof">
    <profRef href="#{translate(., ' ', '-')}" />

(BTW, you can't take a string and treat it like an XPath expression.
If you want to evaluate a string as an XPath expression, then you have
to use an extension function such as dyn:evaluate() from EXSLT.)


Jeni Tennison

 XSL-List info and archive:

Current Thread