I am transforming some XML, renaming each element named alt-title to Running_Head, provided that the attribute alt-title-type is equal to “running-head”.
So, the below code is using the line <xsl:when test="starts-with(@alt-title-type, 'running-head')"> which is working fine. However, when I change this to either of these:
<xsl:when test="ends-with(@alt-title-type, 'running-head')"><xsl:when test="matches(@alt-title-type, 'running-head')">
…this error is thrown:
Error:XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]:
xmlXPathCompiledEval: 2 objects left on the stack.
So, it seems that the function starts-with is working, where ends-with and matches are not.
Here is my XSL, using starts-with, that seems to be working properly:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml" />
<!-- Running_Head -->
<xsl:template match="@*|node()">
<xsl:choose>
<xsl:when test="starts-with(@alt-title-type, 'running-head')">
<xsl:element name="Running_Head">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template> <!-- end of Running_Head -->
</xsl:stylesheet>
…and here is the XML being transformed:
<root-node>
<alt-title alt-title-type="running-head">
This is working
</alt-title>
<alt-title alt-title-type="asdfng-head">
asdfasdf
</alt-title>
<alt-title>
asdfasdf
</alt-title>
<alt-title alt-title-type="running-head">
This is also working
</alt-title>
</root-node>
I am testing this at http://xslt.online-toolz.com/tools/xslt-transformation.php, and http://www.xsltcake.com/.
As other people have pointed out, most XPath 2.0 functions (such as
matches()andends-with()) are not supported by an XSLT 1.0 processor.Moreover, these functions aren’t needed at all in a transformation that implements the current requirements:
when this transformation is applied on the provided XML document:
the wanted, correct reault is produced:
Explanation:
Proper use of templates, match patterns and overriding of the identity rule.