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:
<?xml version="1.0"?>
<stuff>
<element1>
<foo>2</foo>
<bar/>
</element1>
<element2>
<subelement/>
<bar/>
</element2>
<element1>
<foo/>
<bar/>
</element1>
</stuff>
Using:
xalan.exe -p myparam "element1" sample.xml addelement.xslt
I'd like the following result:
<?xml version="1.0"?>
<stuff>
<element1>
<foo>2</foo>
<bar/>
<addedElement/>
</element1>
<element2>
<subelement/>
<bar/>
</element2>
<element1>
<foo/>
<bar/>
<addedElement/>
</element1>
</stuff>
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:
XPathParserException: A node test was expected.
pattern = '$myparam/*[last()]' Remaining tokens are: ('$' 'myparam' '/' '*' '[' 'last' '(' ')' ']') (addelement.xslt, line 12, column 42)
addelement.xslt
<?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>
addelement.xslt with hardcoded path replaced
<?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>
Thanks for helping
To use an XSLT parameterCreate an XsltArgumentList object and add the parameter using the AddParam method. Call the parameter from the style sheet.
XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents. In the transformation process, XSLT uses XPath to define parts of the source document that should match one or more predefined templates.
XSLT <xsl:variable>The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!
The content model of both elements is the same. The way these elements declare variables is the same. However, the value of the variable declared using <xsl:param> is only a default that can be changed with the <xsl:with-param> element, while the <xsl:variable> value cannot be changed.
I don't think you can use variables/paramaters in matching templates like you have coded. Even this doesn't work
<xsl:template match="*[name()=$myparam]/*[last()]">
Instead, try changing the first matching template to as follows, so that the parameter check is inside the template code, not as part of the match statement.
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:if test="local-name() = $myparam">
<addedElement/>
</xsl:if>
</xsl:copy>
</xsl:template>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With