I have some XML that looks like
<?xml version="1.0"?>
<root>
<![CDATA[
> foo
]]>
</root>
(Note the > sign in "> foo") and an XSLT stylesheet
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<foo><xsl:value-of select='.'/></foo>
</xsl:template>
</xsl:stylesheet>
When I run xsltproc stylesheet.xsl data.xml
I get
<?xml version="1.0"?>
<foo>
> foo
</foo>
but the output I want is
<?xml version="1.0"?>
<foo>
> foo
</foo>
i.e. keep the ">" as it is instead of converting it to an entity. How can I accomplish this?
@Oded, @khachik,
Try checking his desired output for well-formedness. It is indeed well-formed XML. ("Valid" is not even a question here, as there is no schema.)
It is a common misconception that ">" is not legal in well-formed XML. In most contexts, "<" is not legal, but ">" is legal everywhere with one rare exception. The relevant paragraph of the spec:
With XSLT 2.0, the "right" way to do what you want is to use
<xsl:character-map>
. With XSLT 1.0, I think the only way to force the use of ">" in the output is to use disable-output-escaping, as @khachik suggested. Note however that XSLT processors are not required to honor DOE or character maps, and some can't (e.g. if they're in a pipeline and are not connected to serialization). But you probably know by now whether yours can, and if it can't, you'll need to handle serialization issues at the end of the pipeline.However, it is worth asking, why do you want the ">" serialized as ">"? As seen in the spec, > is a perfectly acceptable way to express exactly the same information as far as XML is concerned. No downstream XML consumer should know the difference or care. Do you want it for aesthetic reasons?
Update: the OP wants that because the output needs to be not only well-formed XML, it also needs to be well-formed Literate Haskell.
<xsl:value-of select='.' disable-output-escaping="yes"/>
but it wouldn't be well-formed XML.Update With
>
it will be well formed. (With<
it won't.)Adding to the very good explanation of @LarsH:
If your XSLT processor allows DOE, then you can use:
and when this transformation is applied on the provided XML document:
the wanted output is produced: