Allan Chua
Allan Chua

Reputation: 10175

Why does an apostrophe throws me an error on test attribute of xsl:when

I have a scenario where i should check if the education level of an entity is equal to 'Bachelor's Degree/Undergraduate Degree' but the compiler throws me an error saying "Expected end of the expression, found 's '."

Here is my actual code:

        <xsl:when test="education_level='Bachelor's Degree/Undergraduate Degree'">
          <opleiding>
            <xsl:attribute name="id">
              <xsl:value-of disable-output-escaping="yes" select="'30001'"/>
            </xsl:attribute>
            <xsl:value-of disable-output-escaping="yes" select="'Specialisation'"/>
          </opleiding>
        </xsl:when>

Guys help will be appreciated :) thanks in advance

Upvotes: 4

Views: 1533

Answers (5)

Michael Kay
Michael Kay

Reputation: 163262

You need different strategies depending on whether the quote/apostrophe you need to escape is an XML attribute delimiter (the outer quotes) or an XPath string delimiter (the inner quotes).

For XML attribute delimiters, use the XML predefined entities &quot; and &apos;.

For XPath string delimiters, in XPath 2.0 you can escape them by doubling (like in SQL), for example select="'I won''t'". In XPath 1.0 there's no way of escaping string delimiters, so you need a workaround like using variables, or concatenating, or switching your use of double vs single quotes. In practice I generally use variables as you have been shown.

Upvotes: 1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

It is possible to specify the XPath expression in the test attribute without resorting at all to an additional variable and its text-node child:

    <xsl:when test="education_level=&quot;Bachelor&apos;s Degree/Undergraduate Degree&quot;">

Here is a complete transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <xsl:choose>
        <xsl:when test="education_level=&quot;Bachelor&apos;s Degree/Undergraduate Degree&quot;">
            <opleiding>
                <xsl:attribute name="id">
                    <xsl:value-of select="'30001'"/>
                </xsl:attribute>
                <xsl:value-of select="'Specialisation'"/>
            </opleiding>
        </xsl:when>
     </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied to the following XML document:

<t>
 <education_level>Bachelor's Degree/Undergraduate Degree</education_level>
</t>

the wanted, correct result is produced:

<opleiding id="30001">Specialisation</opleiding>

Additional Note: You dont need DOE and in this particular case it will either be ignored or will produce an error -- this is because DOE is allowed only on instructions that create a text node -- not an attribute.

Upvotes: 2

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

You can use variable:

<xsl:variable name="s">Bachelor's Degree/Undergraduate Degree</xsl:variable>

and then:

<xsl:when test="education_level = $s">

Upvotes: 4

Corey Ogburn
Corey Ogburn

Reputation: 24717

Here's the tag as you have it in your question:

test="education_level='Bachelor's Degree/Undergraduate Degree'"

I'll be the first to say that I know nothing about xslt files. XML Style sheets? I'm not sure. Regardless it's purpose, any parser would have the same problem in that you've enclosed a name/value pair as the value of a name/value pair and its causing you problems. The inner single quote (in Bachelor's) isn't being parsed as a literal quotation mark in the string, it's being parsed as the end of the value, i.e. the parser is parsing things as test being the first name, and it's value is the name/value pair of education_level='Bachelor' followed by some stuff it doesn't know what to do with: s Degree/Undergraduate Degree '.

In most languages, you have to escape internal quotation marks so they are seen as characters in the string and not syntactical characters representing the end of the string. Try this for the first tag:

<xsl:when test="education_level='Bachelor\'s Degree/Undergraduate Degree'">

The \ is the escape character in most languages and should be for an xslt as well.

Upvotes: -1

Chris Browne
Chris Browne

Reputation: 1612

Escape the ' in "Bachelor's degree" - you currently have education_level='Bachelor' and then s Degree/Undergraduate Degree'. Try education_level='Bachelor\'s Degree/Undergraduate Degree'

Upvotes: -1

Related Questions