xsl-list
[Top] [All Lists]

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

2002-10-23 03:25:10
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(., ' ', '-')" />
      </xsl:attribute>
    </xsl:element>
  </xsl:for-each>

  
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(., ' ', '-')}" />
  </xsl:for-each>

(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.)
  
Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>