Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output element in comments

Tags:

xslt

I need to display HTML-element in comments (for example)

<!-- <img src="path" width="100px" height="100px"/> -->

I use this approach

<?xml version="1.0" encoding="windows-1251"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="no" encoding="windows-1251"/>

    <xsl:template match="myNode">
        ...
        <xsl:comment><xsl:apply-templates select="image" /></xsl:comment>
        ...
    </xsl:template>

    <xsl:template match="image">
        <img src="{@src}" width="{@width}px" height="{@height}px" />
    </xsl:template>

</xsl:stylesheet>

As a result:

<!---->

that is the code in the element xsl:comment ignored.

How do I display an item in the comments?

like image 905
Kalinin Avatar asked Oct 14 '10 10:10

Kalinin


2 Answers

<xsl:comment><xsl:apply-templates select="image" /></xsl:comment>

As a result:

<!---->

that is the code in the element xsl:comment ignored

The XSLT 1.0 Spec says:

It is an error if instantiating the content of xsl:comment creates nodes other than text nodes. An XSLT processor may signal the error; if it does not signal the error, it must recover by ignoring the offending nodes together with their content.

How do I display an item in the comments?

It depends what is meant for "display": in a browser:

&lt;-- <xsl:apply-templates select="image" /> -->

may be useful, provided the result of <xsl:apply-templates/> aboveis just simple text (not markup).

If to "display" means to provide the result as text, then DOE, if allowed by the XSLT processor, may give us the wanted result:

<-- Some text -->

Finally, if it is required that what should be inside the "comment" should be markup and it should be displayed as markup, then this is rather challenging. In this case one has to use:

<xsl:output method="text"/>

and should present every XML lexical item with its desired serialization (i.e. escaped).

This is how the XPath Visualizer constructs its output.

Here is a small transformation that demonstrates the first two approaches:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
      &lt;-- Hello, World -->

  <xsl:text disable-output-escaping="yes">&lt;--</xsl:text>
   Hello,world! --<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
 </xsl:template>
</xsl:stylesheet>

this transformation, when applied on any XML document (not used), produces:

      &lt;-- Hello, World --&gt;

  <--
   Hello,world! -->

Both "comments" may be viewed as comments in a browser, while only the second is presented as comment in free text.

The third approach (most probably what you want) is illustrated below:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  &lt;-- <xsl:apply-templates select="image"/> -->

 </xsl:template>

 <xsl:template match="image">
  &lt;img src="<xsl:value-of select="@src"/>"
      width="<xsl:value-of select="@width"/>px"
      height="<xsl:value-of select="@height"/>px"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<image src="http://example.com/yyy.jpg" width="200" height="300"/>

the wanted result is produced:

  &lt;-- 
  &lt;img src="http://example.com/yyy.jpg"
      width="200px"
      height="300px"/&gt;
  --&gt;

viewed in a browser as:

<-- <img src="http://example.com/yyy.jpg" width="200px" height="300px"/> -->

like image 175
Dimitre Novatchev Avatar answered Oct 21 '22 05:10

Dimitre Novatchev


It might be possible to replace

<xsl:comment><xsl:apply-templates select="image" /></xsl:comment>

with

<xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
<xsl:apply-templates select="image" />
<xsl:text disable-output-escaping="yes">--&gt;</xsl:text>

Haven't tried though.

like image 40
Michael Avatar answered Oct 21 '22 05:10

Michael