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

时间:2015-12-29 11:10:33

标签: 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>
        <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>

这是XSL(感谢Dimitre):

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


<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:stylesheet>

到目前为止一切顺利。我想

  • 保留换行符和
  • 删除缩进

当我将输出缩进设置为“是”时,我得到两个,换行符和缩进。 当我将输出缩进设置为“no”时,我得不到。

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您不剥离并且不缩进,而是将空白文本节点转换为换行符,如

<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>

那么结果很有希望。

相关问题