Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT: Disable output escaping in an entire document

I'm trying to generate some C# code using xslt - its working great until I get to generics and need to output some text like this:

MyClass<Type>

In this case I've found that the only way to emit this is to do the following:

MyClass<xsl:text disable-output-escaping="yes">&lt;</xsl:text>Type<xsl:text disable-output-escaping="yes">&gt;</xsl:text>

Where:

  • Often it all needs to go on one line, otherwise you end up with line breaks in the generated code
  • In the above example I technically could have used only 1 <xsl:text />, however usually the type Type is given by some other template, e.g:

<xsl:value-of select="@type" />

I don't mind having to write &lt; a lot, but I would like to avoid writing <xsl:text disable-output-escaping="yes">&lt;</xsl:text> for just a single character!

Is there any way of doing disable-output-escaping="yes" for the entire document?

like image 617
Justin Avatar asked May 07 '10 12:05

Justin


2 Answers

The reason why this wasn't working was all down to the I was applying the transform - I was using a XslCompiledTransform and an XmlWriter to transform my xml, however according to Microsoft XML Teams blog as soon as I use an XmlWriter to write my output the tag is ignored!

I fixed this by explicitly setting the XmlWriters output settings to those of the Xsl transform:

XmlWriterSettings ws = xslt.OutputSettings.Clone();
ws.CheckCharacters = false;

xslt.Transform("MyDocument.xml", XmlWriter.Create(Console.Out, ws));

Once I did this, the transform respected my tag, and all was well!

like image 64
Justin Avatar answered Nov 07 '22 05:11

Justin


its working great until I get to generics and need to output some text like this:

MyClass<Type>

In this case I've found that the only way to emit this is to do the following:

MyClass<xsl:text disable-output-escaping="yes">&lt;</xsl:text>Type<xsl:text

disable-output-escaping="yes">>

Certainly, this is not the only way and this is the worst way to produce such result.

Is there any way of doing disable-output-escaping="yes" for the entire document?

This is what the method="text" attribute of <xsl:output> is for. No need of DOE.

So, use the text output method and then:

 MyClass&lt;Type>

Here is a complete small example.

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
  MyClass&lt;Type>
 </xsl:template>
</xsl:stylesheet>

When the above transformation is applied on any XML document (not used), the wanted result is produced:

  MyClass<Type>

I am the author of the XPath Visualizer, which produces IE-like presentation of XML files -- nowhere in thie code there is any DOE.

like image 21
Dimitre Novatchev Avatar answered Nov 07 '22 05:11

Dimitre Novatchev