Jump to content

Execute Xpath Passed In As A String


longrun
 Share

Recommended Posts

Hai all,I have the following problem to solve: The users of my application can specify an xpath like e.g.format-date(current-date(),'[D]/[M]/[Y]', 'en', (), ())this xpath is caught in an xslt file as just being a string (the user could also have typed in a string and then I need this string for sure), but I want this 'string' to be evaluated...so it shoould result in 18/11/2009 instead of the string 'format-date(current-date(),'[D]/[M]/[Y]', 'en', (), ())'So my question is, does anybody know how I execute this xpath if it comes to my function as a string...? With xpath function 'eval' or 'exec' or so??Best regards,longrun

Link to comment
Share on other sites

If the XSLT processor supports it, you can use the dyn:evaluate() EXSLT function. You can see a list of processors that support this at the bottom of the page.

Link to comment
Share on other sites

That is only possible with an extension function. Your expression suggests you use XSLT/XPath 2.0 so below is an XSLT 2.0 stylesheet showing how to solve the problem with Saxon 9 and with AltovaXML tools:

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0"  xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns:saxon="http://saxon.sf.net/"  xmlns:altova="http://www.altova.com/xslt-extensions"  exclude-result-prefixes="xsd saxon altova">      <xsl:template match="test">	<xsl:choose>	  <xsl:when test="function-available('saxon:evaluate')">		<xsl:value-of select="saxon:evaluate(.)"/>	  </xsl:when>	  <xsl:when test="function-available('altova:evaluate')">		<xsl:value-of select="altova:evaluate(.)"/>	  </xsl:when>	</xsl:choose>  </xsl:template></xsl:stylesheet>

It assumes the element containing the XPath expression is named 'test'. Note that saxon:evaluate is not supported by the Saxon 9.2 Home Edition, only by the Professional and Enterprise edition. It is however supported by the Saxon 9.1 B.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...