Zabba
Zabba

Reputation: 65477

Less-than operator causes error "not well-formed" in xsl-if

I'm going through the w3cschools XSLT tutorial, and I am at this page: xsl-if.

On that page (in red) is the text <xsl:if test="price &gt; 10">. This works. I modified the code to use "&lt;" and that works fine too.

I tested <xsl:if test="price > 10"> (note the use of > instead of the &gt;). This works too.

But this fails: <xsl:if test="price < 10">. Error is XML Parsing Error: not well-formed and it points to the < symbol in the expression.

If the > symbol worked fine, why did using the < fail? (I'm using FireFox)

Upvotes: 12

Views: 19488

Answers (3)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243479

If the > symbol worked fine, why did using the < fail? (I'm using FireFox)

Because the "<" character is one of the few that are illegal within an attribute value (it is the start-of tag character).

From the XML Specification

[10]    AttValue    ::=    '"' ([^<&"] | Reference)* '"' 

As can be clearly seen, the "<" and "&" characters are not allowed in any attribute value.

Update: As noticed by @Tomalak, the above should read:

As can be clearly seen, the "<" and "&" characters (unless the latter is part of an entity reference or character reference) are not allowed in any attribute value.

Upvotes: 12

user414661
user414661

Reputation: 1042

You can also see the answer to this on w3schools:

http://www.w3schools.com/xmL/xml_syntax.asp

Entity References

Some characters have a special meaning in XML.

If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.

Upvotes: 6

Tomalak
Tomalak

Reputation: 338248

The unencoded "opening" bracket < is generally invalid in XML attribute values as per the XML spec.

While the "closing" bracket > is allowed, using it is actually bad style (IMHO). XML attribute values have to be XML-encoded, period.

Upvotes: 4

Related Questions