tags:

views:

78

answers:

3

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)

+2  A: 

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.

Dimitre Novatchev
Tomalak
@Tomalak: Thanks, I updated the answer.
Dimitre Novatchev
Alejandro
Tomalak
Alejandro
Dimitre Novatchev
+2  A: 

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.

Tomalak
@Tomalak: Why, using '>" instead of `'>'` makes the code so more readable. I even usually reverse `x < y` to `not(x > y -1)`
Dimitre Novatchev
@Dimitre: That's why I wrote "IMHO". I am quite strict when it comes to value encoding, and I consciously avoid shortcuts and loopholes even if they are documented. `x < y` disturbs my flow of reading not at all. However, seeing `not(x > y -1)` would make me stop and think - even if just for a second. Why should I want that? For me, the increased readability does not outweigh the time I need to think about the expression. For me, it's not economic.
Tomalak
I think that the reverse of `x < y` it should be `y > x`. And is more readable and more economic. Plus, I never understand why `<` could break the parser and `>` don't! Ja!
Alejandro
@Alexandro: No, the reverse of `x < y` is `y >= x` :)
Dimitre Novatchev
@Dimitre Novatchev: No, you're wrong. Let `x` and `y` both be 2. `2 < 2` is false. But `2 >= 2` is true, so the expressions are not equivalent. @Alejandro is correct.
Eric
@Eric: By "reverse" I mean the logical negation: `not(x < y)` is equivalent to `y >= x`
Dimitre Novatchev
+3  A: 

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.