注释中的输出元素

Kal*_*nin 12 xslt

我需要在注释中显示HTML元素(例如)

<!-- <img src="path" width="100px" height="100px"/> -->
Run Code Online (Sandbox Code Playgroud)

我用这种方法

<?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>
Run Code Online (Sandbox Code Playgroud)

结果是:

<!---->
Run Code Online (Sandbox Code Playgroud)

这是xsl:comment忽略元素中的代码.

如何在评论中显示项目?

Mic*_*ael 12

有可能更换

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

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

虽然没试过.


Dim*_*hev 7

<xsl:comment><xsl:apply-templates select="image" /></xsl:comment>
Run Code Online (Sandbox Code Playgroud)

结果是:

<!---->
Run Code Online (Sandbox Code Playgroud)

这是元素xsl中的代码:注释被忽略

XSLT 1.0规范:

如果实例化xsl的内容,则会出错:comment会创建除文本节点之外的节点.XSLT处理器可能会发出错误信号; 如果它没有发出错误信号,它必须通过忽略违规节点及其内容来恢复.

如何在评论中显示项目?

这取决于"显示"的含义:在浏览器中:

&lt;-- <xsl:apply-templates select="image" /> -->
Run Code Online (Sandbox Code Playgroud)

可能有用,只要上面的结果<xsl:apply-templates/>只是简单的文本(不是标记).

如果"显示"表示将结果作为文本提供,那么DOE(如果XSLT处理器允许)可以给我们想要的结果:

< - 一些文字 - >

最后,如果要求"注释"中应包含的内容应该是标记,并且它应该显示为标记,那么这是相当具有挑战性的.在这种情况下,必须使用:

<xsl:output method="text"/>
Run Code Online (Sandbox Code Playgroud)

并且应该为每个XML词汇项目提供其所需的序列化(即转义).

这就是XPath Visualizer构造其输出的方式.

这是一个小变换,演示了前两种方法:

<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>
Run Code Online (Sandbox Code Playgroud)

这种转换,当应用于任何XML文档(未使用)时,会产生:

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

  <--
   Hello,world! -->
Run Code Online (Sandbox Code Playgroud)

两个"评论"可以在浏览器中被视为评论,而只有第二个在自由文本中被显示为评论.

第三种方法(最可能是你想要的)如下图所示:

<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>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于以下XML文档时:

<image src="http://example.com/yyy.jpg" width="200" height="300"/>
Run Code Online (Sandbox Code Playgroud)

产生了想要的结果:

  &lt;-- 
  &lt;img src="http://example.com/yyy.jpg"
      width="200px"
      height="300px"/&gt;
  --&gt;
Run Code Online (Sandbox Code Playgroud)

在浏览器中查看:

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