Prevent Using XslCompiledTransform Self-Closing Tags

I am using XslCompiledTransform to convert an XML file to HTML. Is there a way to prevent the use of self-closing tags.

eg.

 <span></span> <!-- I want this even if content empty --> <span/> <!-- stop doing this! -> 

Self-closing tags with a space will ruin my document no matter which browser I use, although it is valid XML, just for this "span" it is not allowed to have self-closing tags.

Is there a parameter that I can set in my xsl or in my C # .Net code to prevent the use of self-closing tags?

+6
source share
4 answers

Although I could not classify this as a direct solution (since it does not emit an empty element), the workaround I used was to put a space (using xsl: text) in the element - since this is HTML markup, and if you activate standard mode (not quirks), the additional space does not change the displayed content. I also had no control over calling the transform object.

 <div class="clearBoth"><xsl:text> </xsl:text></div> 
+1
source

You can try <xsl:output method="html"/> , however the result will no longer be a valid XML document.

Or you can call the XslCompiledTransform.Transform () method , passing as one of the parameters of your own XmlWriter . In your implementation, you are in full control and can implement any necessary serialization of the result tree.

0
source

In your XSLT, use <xsl:output method="html"/> and then make sure your HTML result elements created by your stylesheet do not have a namespace. Also, depending on how you use XslCompiledTransform in C # code, you need to make sure that the xsl:output parameters in the stylesheet are respected. You can easily achieve this by converting to a file or stream or TextWriter, in which case nothing needs to be done. However, if for some reason you switch to XmlWriter, you need to make sure that it is created with the appropriate settings, for example.

 XslCompiledTransform proc = new XslCompiledTransform(); proc.Load("sheet.xsl"); using (XmlWriter xw = XmlWriter.Create("result.html", proc.OutputSettings)) { proc.Transform("input.xml", null, xw); } 

But usually you should be fine just converting to Stream or TextWriter, in which case you donโ€™t need anything in the C # code to honor the output method in the stylesheet.

0
source

The only solution I could find was to add logic to the XSL file. Basically, if the elements that I wanted to wrap around are empty, do not use the span element at all.

 <xsl:if test="count(jar/beans) > 0"> <xsl:apply-templates select="jar/beans"/> </xsl:if> 

You shouldnโ€™t paste this everywhere in my xsl file to compensate for the fact that although I choose the "html" output method, it will more than willingly generate illegal HTML.

Sigh.

0
source

Source: https://habr.com/ru/post/895632/


All Articles