Software is like sex... It's better when it's free.

Blog - Form an XPath expression dynamically

Form an XPath expression dynamically

For fans of the XML/XSL/XPath combo, a small trick when you push it the limits of generic to form an XPath expression dynamically.

Suppose a xsl variable contains an "end" of the XPath expression (one node), this would give us this:

<!--
  - Select a xml node with a dynamic xPath expression
  -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:template match="root">
    <xsl:variable name="dynamicNode" select="'user_name'" />

    <!-- this will give this xPath = "//root/bdd/users[1]/user_name" -->
    <xsl:value-of select="bdd/users[1]/*[name()=$dynamicNode]" />

  </xsl:template>
</xsl:stylesheet>
Tags:  xmlxpathxsl.
Posted the Tuesday 28 april 2009 20:39:02