title: How to use a parameter in a xslt as a XPath?
body: <p>I'd like to add an element to a xml document and I'd like to pass as a parameter the path to the element.</p>
  
  <p>sample.xml file:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;stuff&gt;
    &lt;element1&gt;
      &lt;foo&gt;2&lt;/foo&gt;
  &lt;bar/&gt;
    &lt;/element1&gt;
    &lt;element2&gt;
  &lt;subelement/&gt;
  &lt;bar/&gt;
     &lt;/element2&gt;
     &lt;element1&gt;
       &lt;foo/&gt;
   &lt;bar/&gt;
     &lt;/element1&gt;
   &lt;/stuff&gt;
  </code></pre>
  
  <p>Using:</p>
  
  <pre><code>xalan.exe -p myparam "element1" sample.xml addelement.xslt
  </code></pre>
  
  <p>I'd like the following result: </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;stuff&gt;
    &lt;element1&gt;
      &lt;foo&gt;2&lt;/foo&gt;
      &lt;bar/&gt;
      &lt;addedElement/&gt;
    &lt;/element1&gt;
    &lt;element2&gt;
  &lt;subelement/&gt;
  &lt;bar/&gt;
     &lt;/element2&gt;
     &lt;element1&gt;
       &lt;foo/&gt;
   &lt;bar/&gt;
       &lt;addedElement/&gt;
     &lt;/element1&gt;
   &lt;/stuff&gt;
  </code></pre>
  
  <p>I've manage to write addelement.xslt, when hardcoding the path it works, but when I try to use
  parameter myparam in the match attribute I get:</p>
  
  <pre><code>XPathParserException: A node test was expected.
  pattern = '$myparam/*[last()]' Remaining tokens are:  ('$' 'myparam' '/' '*' '[' 'last' '(' ')' ']') (addelement.xslt, line 12, column 42)
  </code></pre>
  
  <p>addelement.xslt</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
  &lt;xsl:template match="@*|node()"&gt;
    &lt;xsl:copy&gt;
      &lt;xsl:apply-templates select="@*|node()"/&gt;
    &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="element1/*[last()]"&gt;
      &lt;xsl:copy-of select="."/&gt;
  &lt;addedElement&gt;&lt;/addedElement&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>addelement.xslt with hardcoded path replaced</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
  &lt;xsl:param name="myparam"/&gt;
  
  &lt;xsl:template match="@*|node()"&gt;
    &lt;xsl:copy&gt;
      &lt;xsl:apply-templates select="@*|node()"/&gt;
    &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="$myparam/*[last()]"&gt;
      &lt;xsl:copy-of select="."/&gt;
  &lt;addedElement&gt;&lt;/addedElement&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Thanks for helping</p>