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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5737862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:40:08  来源:igfitidea点击:

XSLT output formatting: removing line breaks, and blank output lines from removed elements while keeping indent

xmlxslt

提问by Zori

Here is my XML:

这是我的 XML:

<doc xmlns="http://www.foo.org">
  <div>
    <title>Mr. Title</title>
    <paragraph>This is one paragraph.
    </paragraph>
    <paragraph>Another paragraph.
    </paragraph>
    <list>
      <orderedlist>
        <item>
          <paragraph>An item paragraph.</paragraph>
        </item>
        <item>
          <paragraph>Another item paragraph</paragraph>
        </item>
      </orderedlist>
    </list>
  </div>    
</doc>

Here is my XSL:

这是我的 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>

And the output:

和输出:

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

I would like to change 3 things about this output:

我想更改有关此输出的 3 件事:

  1. remove the line break from the "p" elements (originally paragraph)
  2. remove the line breaks from the "li" elements (produced when item/paragraph elements were removed)
  3. remove the extra blank lines created when the list items were removed
  1. 从“p”元素中删除换行符(最初是段落)
  2. 从“li”元素中删除换行符(在删除项目/段落元素时产生)
  3. 删除列表项被删除时创建的额外空行

-I have tried <xsl:template match="foo:list/text()[normalize-space(.)='']" />for #3, but this messes with the indentation

-我试过<xsl:template match="foo:list/text()[normalize-space(.)='']" />#3,但这与缩进混乱

-I have also tried <xsl:template match="foo:paragraph/text()[normalize-space(.)='']" />for #1, but this has no effect on the line breaks

- 我也试过<xsl:template match="foo:paragraph/text()[normalize-space(.)='']" />#1,但这对换行符没有影响

-And I have tried <xsl:strip-space elements="*"/>but this eliminates all indentation

- 我已经尝试过,<xsl:strip-space elements="*"/>但这消除了所有缩进

Thank you!!

谢谢!!

回答by Mads Hansen

Adding these templates to your stylesheet:

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

<xsl:template match="*/text()[normalize-space()]">
    <xsl:value-of select="normalize-space()"/>
</xsl:template>

<xsl:template match="*/text()[not(normalize-space())]" />

Produces this output:

产生这个输出:

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

回答by Dimitre Novatchev

At the very end of the stylesheet add these two templates:

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

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

You now get the wanted result:

你现在得到了想要的结果

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