【问题标题】:Comment a single node注释单个节点
【发布时间】:2017-08-06 14:12:44
【问题描述】:

使用 XSLT,我如何评论单个节点而不评论其子节点?

我有这个 html:

<html>
  <body>
    <div class="blah" style="blahblah">
      <span>
        <p>test</p>
      </span>
    </div>
  </body>
</html>

我想要这个输出:

<html>
  <body>
    <!-- div class="blah" style="blahblah" -->
      <span>
        <p>test</p>
      </span>
    <!-- /div -->
  </body>
</html>

关键是子节点被复制,被注释节点的所有属性也被复制。

以下是我最好的尝试,但不起作用。 XSLT 处理器喊道: “在添加了文本、注释、pi 或子元素节点后,不能将属性和命名空间节点添加到父元素。”

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <!-- IdentityTransform -->
    <xsl:template match="/ | @* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
    </xsl:template>

    <xsl:template match="div">
      <xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
      <xsl:copy>
        <xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
          <xsl:apply-templates select="@* | node()"/>
        <xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
      </xsl:copy>
      <xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

【问题讨论】:

    标签: xslt


    【解决方案1】:

    值得指出的是,您可以只使用&lt;xsl:comment&gt; 在输出 XML 中创建 cmets,而不必担心正确关闭它们。如果没有正确放置结束注释分隔符,您所做的很容易导致问题。

    这样就可以了:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <!-- IdentityTransform -->
        <xsl:template match="/ | @* | node()">
          <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
          </xsl:copy>
        </xsl:template>
    
        <xsl:template match="div">
          <xsl:comment>
              <xsl:text> div </xsl:text>
              <xsl:for-each select="@*">
                   <xsl:value-of select="local-name()"/>
                   <xsl:text>="</xsl:text>
                   <xsl:value-of select="."/>
                   <xsl:text>" </xsl:text>
               </xsl:for-each>
          </xsl:comment>
          <xsl:apply-templates select="./*" />
          <xsl:comment> /div </xsl:comment>
        </xsl:template>
    
    </xsl:stylesheet>
    

    当输出打印得很漂亮时,它会给出:

    <html>
       <body>
          <!-- div class="blah" style="blahblah" -->
          <span>
             <p>test</p>
          </span>
          <!-- /div -->
       </body>
    </html>
    

    【讨论】:

    • 谢谢乐高!我希望我可以选择多个答案,因为您的答案似乎提供了一种更标准的方式,因为它使用了
    【解决方案2】:

    这是对 XSLT 的一种非常可怕的使用,但这似乎可行,而且我想不出更简洁的方法:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <!-- IdentityTransform -->
      <xsl:template match="/ | @* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="div">
        <xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
          <xsl:apply-templates select="node()"/>
          <xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
        </xsl:copy>
        <xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
      </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 我无法想象你为什么不使用&lt;xsl:comment&gt;
    • 接受这个答案,因为它比乐高的短,而且看起来更简单。
    • @torazaburo 我试过了,但无法让它工作。另外,它不会造成不好的嵌套吗?
    • @torazaburo 我没有使用&lt;:xsl:comment&gt;,因为这需要手动构建标签和属性,而不是让 XSLT 处理器来做。
    • @JLRishe 您已经在为评论手动创建标签了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 2016-12-10
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多