2

Here is my XSLT file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="a/b" />
</xsl:template>
</xsl:stylesheet>

Here is my XML file:

<a><b>four &lt; five</b></a>

I would like the output

four < five

but I get

four

Clearly there is a problem with XSLT understanding the &lt; entity. I have read this question

but do not find it quite relevant because it pertains to the xsl:text tag, whereas I am interested in the xsl:value-of tag.

By the way, I am unable to modify the XML file.

6
  • You're showing you transformation result in a browser? If so, check your page source, the < character should be there but browser understood it as a broken markup. Commented Apr 9, 2014 at 10:22
  • Thanks Rubens, I'd forgotten that! In which case, I guess I should have said that I want XSLT to produce four &lt; five, so that my browser can then produce four < five. Commented Apr 9, 2014 at 10:45
  • I cannot reproduce your issue. Your stylesheet generates a result of <?xml version="1.0" encoding="utf-8"?>four &lt; five. Commented Apr 9, 2014 at 10:50
  • @michael.hor257k Oh, that's odd. I used w3schools.com/xsl/… . If you copy my XML snippet into the left-hand box, and my XSLT snippet into the right-hand box, does it work for you? Commented Apr 9, 2014 at 11:25
  • 4
    This is most likely w3schools being rubbish. (If you enter <a><b>four &amp;lt; five</b></a> you will should get your expected output, although this is not the correct thing to do!). Try it at xmlplayground.com or xsltransform.net and you will see there is actually no problem with your code at all. Commented Apr 9, 2014 at 11:40

1 Answer 1

3

This is most likely w3schools being not up to a good standard. If you enter <a><b>four &amp;lt; five</b></a> you will should get your expected output, although this is not the correct thing to do!

Using &lt; as the entity is the correct thing to do in the XML. And when the resultant XML gets serialised, it should also be output as &lt;

Try it at xmlplayground.com or xsltransform.net and you will see there is actually no problem with your code at all.

As an aside, if you are intending to show the results in the browser, you should probably add the xsl:output statement to indicate you are outputting HTML. (And to stop the <?xml version="1.0" encoding="utf-8"?> declaration being output).

<xsl:output method="html" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.