Recker Recker - 5 months ago 13
Perl Question

xsl:copy-of : could not compile select expression

I am trying to transform an xml file using xsl stylesheet.

xml file

<?xml version="1.0" encoding="utf-8"?>
<msg>
<ent key="key1">
<text>Error: Could not find </text>
<translation>Another Error similar to previous one.</translation>
</ent>
</msg>


xsl file

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="msg">
<xsl:for-each select="ent">
<xsl:variable name="current_key" select="@key"/>
<xsl:variable name="Match" select="$translations/msg/ent[@key = $current_key]"/>
<xsl:choose>
<xsl:when test="$Match and normalize-space($Match/text) = normalize-space(.) and (not(@translate) or @translate = true())">
<xsl:copy>
<xsl:copy-of select="$Match/(@key|translation/text())|@context"/>
</xsl:copy>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


@translate
is one of the attributes but I have not included it in the example xml file.

I am using Perl to do my transformation.
I use the script as mentioned in this answer, when I get the following error,

Invalid expression
compilation error: file test.xsl line 11 element copy-of
xsl:copy-of : could not compile select expression '$Match/(@key|translation/text())|@context'
at Transform.pl line 10


Why is the
select
expression causing problem?

Answer

Using $Match/(@key|translation/text()) is allowed in XPath 2.0 as used in XSLT 2.0 but Perl is likely to use an XSLT 1.0 processor like libxslt. So you would need to spell it out as $Match/@key | $Match/translation/text().

Comments