How to use a parameter in a xslt as a XPath?

pedrospider

New Member
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.sample.xml file:\[code\]<?xml version="1.0"?><stuff> <element1> <foo>2</foo><bar/> </element1> <element2><subelement/><bar/> </element2> <element1> <foo/> <bar/> </element1> </stuff>\[/code\]Using:\[code\]xalan.exe -p myparam "element1" sample.xml addelement.xslt\[/code\]I'd like the following result: \[code\]<?xml version="1.0"?><stuff> <element1> <foo>2</foo> <bar/> <addedElement/> </element1> <element2><subelement/><bar/> </element2> <element1> <foo/> <bar/> <addedElement/> </element1> </stuff>\[/code\]I've manage to write addelement.xslt, when hardcoding the path it works, but when I try to useparameter myparam in the match attribute I get:\[code\]XPathParserException: A node test was expected.pattern = '$myparam/*[last()]' Remaining tokens are: ('$' 'myparam' '/' '*' '[' 'last' '(' ')' ']') (addelement.xslt, line 12, column 42)\[/code\]addelement.xslt\[code\]<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy></xsl:template><xsl:template match="element1/*[last()]"> <xsl:copy-of select="."/><addedElement></addedElement></xsl:template></xsl:stylesheet>\[/code\]addelement.xslt with hardcoded path replaced\[code\]<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:param name="myparam"/><xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy></xsl:template><xsl:template match="$myparam/*[last()]"> <xsl:copy-of select="."/><addedElement></addedElement></xsl:template></xsl:stylesheet>\[/code\]Thanks for helping
 
Top