Why does the apostrophe give me an error in the test xsl: when attribute

I have a scenario where I have to check if the entity’s education level is “Bachelor’s Degree / Bachelor’s Degree”, but the compiler gives me the error message “Expected end of expression found”.

Here is my actual code:

<xsl:when test="education_level='Bachelor 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 will help to evaluate :) thanks in advance

+4
source share
5 answers

You can use the variable:

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

and then:

 <xsl:when test="education_level = $s"> 
+4
source

You can specify an XPath expression in the test attribute, without resorting to an additional variable and its text node child :

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

Here is the complete conversion:

 <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 conversion is applied to the following XML document :

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

required, the correct result is obtained :

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

Additional note : you do not need DOE, and in this particular case it will either be ignored or cause an error - this is because DOE is allowed only by instructions that create the node text - not an attribute.

+2
source

You need different strategies, depending on what the quote / apostrophe should be, which you need to avoid, is an XML attribute delimiter (external quotation marks) or an XPath string delimiter (internal quotation marks).

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

For XPath line separators in XPath 2.0, you can avoid doubling them (for example, in SQL), for example select="'I won''t'" . There is no way to avoid line breaks in XPath 1.0, so you need a workaround, such as using variables or concatenating, or switching between double and single quotes. In practice, I usually use variables, as you have shown.

+1
source

Take it "to" bachelor's degree "- you currently have education_level='Bachelor' and then s Degree/Undergraduate Degree' . Try education_level='Bachelor\ Degree/Undergraduate Degree'

-1
source

Here is the tag you have in your question:

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

I will be the first to say that I do not know anything about xslt files. XML style sheets? I'm not sure. Regardless of the purpose, any parser will have the same problem that you entered a name / value pair as the value of the name / value pair and caused you problems. The internal single quote (in the Bachelor) is not parsed as the literal label of the quote in the string, it is parsed as the end of the value, i.e. The parser parses things like test , which is the first name, and its value is a pair name / value education_level='Bachelor' , followed by some things that it does not know what to do with: s Degree/Undergraduate Degree ' .

In most languages, you need to avoid the inner quotation marks so that they appear as characters in a string, rather than syntactic characters representing the end of a string. Try this for the first tag:

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

\ is an escape character in most languages ​​and should also be for xslt.

-1
source

All Articles