In the following XSL transformation how do I output the '<' and '>' symbol?
Input XML:
<TestResult bugnumber="3214" testname="display.methods::close->test_ManyInvoke" errortype="Failure"><ErrorMessage><![CDATA[calling close() method failed - expected:<2>]]></ErrorMessage>
XSLT:
<xsl:template match="TestResult">
<xsl:variable name="errorMessage">
<xsl:value-of select="ErrorMessage" disable-output-escaping="yes"/>
</xsl:variable>
<Test name='{@testname}'>
<TestResult>
<Passed>false</Passed>
<State>failure</State>
<Metadata>
<Entry name='bugnumber' value='{@bugnumber}' />
</Metadata>
<TestOutput>
<Metadata>
<Entry name='ErrorMessage' value='{$errorMessage}' />
</Metadata>
</TestOutput>
</TestResult>
</Test>
</xsl:template>
Output XML:
<Test name="display.methods::close->test_ManyInvoke">
<TestResult>
<Passed>false</Passed>
<State>failure</State>
<Metadata>
<Entry name="bugnumber" value="3214"/>
</Metadata>
<TestOutput>
<Metadata>
<Entry name="ErrorMessage" value="calling close() method failed - expected:<2>"/>
</Metadata>
</TestOutput>
</TestResult>
</Test>
Short answer: You can't.
Long answer: The value of attributes cannot contain a few special characters, such as '<'
, '>'
and '&'
.
If present, they are escaped as: '<'
, '>'
and '&'
.
These characters can be produced if the output method is 'text', which is not your case.
Text that looks like these characters when displayed in a browser can also be produced: use: '<'
, '>'
and '&'
.
Finally, you can produce these characters as part of a text node using CDATA section. The following example illustrates this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output cdata-section-elements="t"
omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<t> < & ></t>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on any XML document (not used), the result is:
<t><![CDATA[ < & >]]></t>
This is a really old question, and the other commenters are probably right to question why you need this, but if you just want to know how to add a < or > into your output here's how you do it.
<xsl:value-of disable-output-escaping="yes" select="string('<')"/>
More info at the MSDN site.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With