问题
Sorry for my English.
XSL 1.0. How can I to calculate expression from element or attribute value?
For example XML:
<position>
<localizedName>ref-help</localizedName>
<reference>concat('../help/', $lang, '/index.html')</reference>
</position>
I try use expression from 'reference' attribute:
<xsl:for-each select="/content/positions/position">
<li>
<!--Save expression to variable...-->
<xsl:variable name="path" select="reference"/>
<!--Evaluate variable and set it value to 'href'-->
<a target="frDocument" href="{$path}">
<xsl:variable name="x" select="localizedName"/>
<xsl:value-of select="$resources/lang:resources/lang:record[@id=$x]"/>
</a>
</li>
</xsl:for-each>
But I get string:
file:///C:/sendbox/author/application/support/concat('../help/',%20%24lang,%20'/index.html')
How can I evaluate it?
Regards
回答1:
If your XSLT processor implements the EXSLT extensions, you can reference a function that dynamically evaluates strings as XPath expressions:
<xsl:stylesheet
version="1.0"
xmlns="http://www.w3.org/1999/XSL/Transform"
xmlns:dyn="http://exslt.org/dynamic"
extension-element-prefixes="dyn"
>
<xsl:template match="content">
<xsl:apply-templates select="positions/position" />
</xsl:template>
<xsl:template match="position">
<li>
<a target="frDocument" href="{dyn:evaluate(reference)}">
<xsl:value-of select="
$resources/lang:resources/lang:record[@id=current()/localizedName]
"/>
</a>
</li>
</xsl:template>
</xsl:stylesheet>
Note:
- there is no need to save things in a variable before using them
- there is a
current()
function you might have missed - use
<xsl:apply-templates>
and<xsl:template>
in favor of<xsl:for-each>
来源:https://stackoverflow.com/questions/10046104/xsl-evaluate-expression