Xml XSLT输出格式:从删除的元素中删除换行符和空白输出行,同时保持缩进

Xml XSLT输出格式:从删除的元素中删除换行符和空白输出行,同时保持缩进,xml,xslt,Xml,Xslt,以下是我的XML: Title先生 这是一段。 另一段。 段落中的项目。 另一个项目第段 以下是我的XSL: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:foo="http://www.foo.org"> <xsl:output method="xml"

以下是我的XML:


Title先生
这是一段。
另一段。
段落中的项目。
另一个项目第段
以下是我的XSL:

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

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

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

 <xsl:template match="foo:doc">
  <xsl:element name="newdoc" namespace="http://www/w3.org/1999/xhtml">
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="foo:div">
  <segment title="{foo:title}">
   <xsl:apply-templates/>
  </segment>
 </xsl:template>

 <xsl:template match="foo:title">
  <xsl:element name="h2">
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="foo:paragraph">
  <xsl:element name="p">
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="foo:list">
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="foo:orderedlist">
  <xsl:element name="ol">
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="foo:item">
  <xsl:element name="li">
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="foo:item/foo:paragraph">
  <xsl:apply-templates/>
 </xsl:template>

</xsl:stylesheet>

以及输出:

<newdoc xmlns="http://www/w3.org/1999/xhtml">
  <segment xmlns="" title="Mr. Title">
    <h2>Mr. Title</h2>
    <p>This is one paragraph.
    </p>
    <p>Another paragraph.
    </p>

      <ol>
        <li>
          An item paragraph.
        </li>

        <li>
          Another item paragraph
        </li>
      </ol>

  </segment>    
</newdoc>

Title先生
这是一段。

另一段。

  • 段落中的项目。
  • 另一个项目第段
  • 我想更改此输出的3个方面:

  • 从“p”元素(最初为段落)中删除换行符
  • 从“li”元素中删除换行符(删除项目/段落元素时生成)
  • 删除删除列表项时创建的额外空行
  • -我尝试了#3的
    ,但这会弄乱缩进

    -我还尝试了#1的
    ,但这对换行符没有影响

    -我试过
    ,但这消除了所有缩进


    谢谢

    将这些模板添加到样式表中:

    <xsl:template match="*/text()[normalize-space()]">
        <xsl:value-of select="normalize-space()"/>
    </xsl:template>
    
    <xsl:template match="*/text()[not(normalize-space())]" />
    
    
    
    生成此输出:

    <?xml version="1.0" encoding="UTF-8"?>
    <newdoc xmlns="http://www/w3.org/1999/xhtml">
        <segment xmlns="" xmlns:foo="http://www.foo.org" title="Mr. Title">
            <h2>Mr. Title</h2>
            <p>This is one paragraph.</p>
            <p>Another paragraph.</p>
            <ol>
                <li>An item paragraph.</li>
                <li>Another item paragraph</li>
            </ol>
        </segment>
    </newdoc>
    
    
    Title先生
    这是一段

    另一段

  • 段落中的项目
  • 另一个项目第段

  • 在样式表的最后添加这两个模板

    <xsl:template match=
    "text()[not(string-length(normalize-space()))]"/>
    
    <xsl:template match=
    "text()[string-length(normalize-space()) > 0]">
      <xsl:value-of select="translate(.,'&#xA;&#xD;', '  ')"/>
    </xsl:template>
    
    <?xml version="1.0" encoding="UTF-8"?>
    <newdoc xmlns="http://www/w3.org/1999/xhtml">
       <segment xmlns="" xmlns:foo="http://www.foo.org" title="Mr. Title">
          <h2>Mr. Title</h2>
          <p>This is one paragraph.         </p>
          <p>Another paragraph.         </p>
          <ol>
             <li>An item paragraph.</li>
             <li>Another item paragraph</li>
          </ol>
       </segment>
    </newdoc>
    
    
    
    您现在可以获得想要的结果了

    <xsl:template match=
    "text()[not(string-length(normalize-space()))]"/>
    
    <xsl:template match=
    "text()[string-length(normalize-space()) > 0]">
      <xsl:value-of select="translate(.,'&#xA;&#xD;', '  ')"/>
    </xsl:template>
    
    <?xml version="1.0" encoding="UTF-8"?>
    <newdoc xmlns="http://www/w3.org/1999/xhtml">
       <segment xmlns="" xmlns:foo="http://www.foo.org" title="Mr. Title">
          <h2>Mr. Title</h2>
          <p>This is one paragraph.         </p>
          <p>Another paragraph.         </p>
          <ol>
             <li>An item paragraph.</li>
             <li>Another item paragraph</li>
          </ol>
       </segment>
    </newdoc>
    
    
    Title先生
    这是一段

    另一段

  • 段落中的项目
  • 另一个项目第段

  • 我已经修好了,谢谢!但是,我没有得到与您相同的输出。。。我仍然在“li”和“ol”元素前后的空行中得到换行符。似乎在移除元素的任何位置都不会受到影响。是否有更好的方法来删除不留空间的元素?嗯,当我使用AltovaXML通过Xselerator运行它时,它生成了我发布的没有空格行的输出,但我通过Saxon运行了它们。我添加了另一个只匹配空格的
    text()
    节点的模板,该节点生成发布的输出并应处理其他项目。“使用AltovaXML”意味着只删除空格的文本节点,与使用
    xsl:strip space elements=“*”
    相同。添加
    行可消除缩进(将所有内容都放在一行中)正如
    xsl:strip space elements=“*”
    所做的那样。顺便说一句,我使用的是Firefox的XSLT。添加它的一个有趣的副作用是(至少在我的情况下)该过程现在运行得更快(使用xsltproc):从20秒降到0.1秒(在我的情况下,一个手写过滤器将36k行HTML转换为LaTeX).我非常惊讶我再次检查了计时!您写道“我已经尝试了
    ,但这消除了所有缩进”这仅适用于输入源。顺便说一句,它解决了问题2和3。要解决问题1,您需要@Mads Hansen的答案中建议的
    。问题再次好,+1。请参阅我的答案,以获得一个简短而简单的解决方案。:)这也会删除所有缩进(将所有内容都放在一行中)。这是因为我使用的是Firefox XSLT吗?@Zori:我已经用9个不同的处理器运行了转换——没有一个会丢失缩进。我没有FF XSLT处理器:(当然,Firefox的默认处理器正在进行格式化。我安装了另一个处理器,所有的答案都很好。很抱歉麻烦,谢谢你的帮助!”佐莉:很高兴在最后你找到了问题的原因。你可以考虑现在接受一个答案: