首页 > 解决方案 > 删除单个节点上的缩进

问题描述

我希望在不添加缩进 =“否”的情况下调整单个节点。

<xsl:output method="html" indent="no" html-version="5"/>

在测试“normalize-space”和“translate”时,它们工作正常,假设 indent 设置为 no,但该设置会影响整个文档。

问题: 如何删除单行的缩进?

您可以在此处找到代码示例: https ://xsltfiddle.liberty-development.net/6qaHaQL/1

下面是相同的代码:

XML:

<?xml version="1.0" encoding="utf-8" ?>

<data
xmlns:base="http://www.example.org/1"
    >
<base:Content book="Alice">
    <chapterOne>
        Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading.
    </chapterOne>
</base:Content>

<base:Content book="Alice">
    <chapterTwo>
        There seemed to be no use in waiting by the little door, so she went back to the table, half hoping she might find another key on it, or at any rate a book of rules for shutting people up like telescopes
    </chapterTwo>
</base:Content>
</data>

XSL:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:base="http://www.example.org/1"
>

  <!--xsl:output method="html" indent="no" html-version="5"/>-->
  <xsl:output method="html" html-version="5"/>

  <xsl:template match="/data">
      
      <html>
          <body>
              
              Chapter one:
              
              <xsl:value-of select="normalize-space(base:Content/chapterOne)"/>
              
              <!-- Test [1] Use [normalize-space]. Works if adding [indent="no"] -->
              <!--xsl:value-of select="normalize-space(base:Content/chapterOne)"/>-->
              
              <!-- Test [2] Use [translate]. Works if adding [indent="no"] -->
              <!--xsl:value-of select="translate(base:Content/chapterOne, '&#xA;', '')"/>-->
              
              Chapter two:
              <xsl:value-of select="base:Content/chapterTwo"/>
              
          </body>
      </html>

  </xsl:template>
  
</xsl:stylesheet>

当前结果:

<!DOCTYPE HTML>
<html xmlns:base="http://www.example.org/1">
   <body>
      
      Chapter one:
      
      Alice was beginning to get very tired of sitting by her sister on the bank, and of
      having nothing to do: once or twice she had peeped into the book her sister was reading.
      
      
      
      
      
      
      
      Chapter two:
      
      There seemed to be no use in waiting by the little door, so she went back to the table,
      half hoping she might find another key on it, or at any rate a book of rules for shutting
      people up like telescopes
      
   </body>
</html>

想要的结果

<!DOCTYPE HTML>
<html xmlns:base="http://www.example.org/1"><body>
              
              Chapter one:
              
              Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading.
              
              Chapter two:
              
  There seemed to be no use in waiting by the little door, so she went back to the table,
  half hoping she might find another key on it, or at any rate a book of rules for shutting
  people up like telescopes
    </body></html>

标签: xmlxsltxslt-2.0

解决方案


推荐阅读