Xml XSLT输出格式:保留换行符,删除缩进

Xml XSLT输出格式:保留换行符,删除缩进,xml,xslt,indentation,line-breaks,Xml,Xslt,Indentation,Line Breaks,以下是我的XML: <doc> <article> <texte> <notes>-</notes> <content> <title>T 1</title> <argument>Arg 1</argument> <p>Paragraph 1.1</p>

以下是我的XML:

<doc>
  <article>
    <texte>
      <notes>-</notes>
      <content>
        <title>T 1</title>
        <argument>Arg 1</argument>
        <p>Paragraph 1.1</p>
        <p>Paragraph 1.2</p>
        <p>Paragraph <i>1.3</i></p>
        <short-author>FB</short-author>
      </content>
      <notes>-</notes>
      <content>
        <title>T2</title>
        <p>Paragraph 2.1</p>
        <short-author>JD</short-author>
      </content>
      <notes>-</notes>
      <content>
        <title>T3</title>
        <argument>Arg 3</argument>
        <p>Paragraph 3.1</p>
        <short-author>NC</short-author>
      </content>
    </texte>
  </article>
</doc>

-
T1
Arg 1
第1.1段

第1.2段

第1.3段

食品饮料 - T2 第2.1段

法学博士 - T3 Arg 3 第3.1段

数控
这是XSL(感谢Dimitre):


到目前为止还不错。我想

  • 保留换行符和
  • 去掉压痕
当我将输出缩进设置为“是”时,我同时得到换行和缩进。 当我将输出缩进设置为“否”时,我得不到任何缩进


谢谢大家!

如果不剥离和缩进,而是将空白文本节点转换为换行符,如中所示

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

<xsl:template match="comment()"/>

<xsl:template match="@class" />

  <xsl:template match="node()[not(self::doc|self::texte|self::author|self::style|self::span)]|@*" >
    <xsl:copy>
      <xsl:apply-templates select="node()[not(self::author)]|@*"/>
    </xsl:copy>
  </xsl:template>

<xsl:template match="text()[not(normalize-space())]">
    <xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>




然后结果就是你想要的。

谢谢,这很有效,但我认为还没有结束。知道我如何有选择地省略换行符吗,例如在

之间?此外,此方法会在节点被省略的地方创建空行(
)。你知道如何摆脱它们吗?至于你的第一条评论,如果你添加了例如
你在使用哪个XSLT处理器?
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="comment()"/>

<xsl:template match="@class" />

  <xsl:template match="node()[not(self::doc|self::texte|self::author|self::style|self::span)]|@*" >
    <xsl:copy>
      <xsl:apply-templates select="node()[not(self::author)]|@*"/>
    </xsl:copy>
  </xsl:template>

<xsl:template match="text()[not(normalize-space())]">
    <xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>