XSLT 输出格式:从删除的元素中删除换行符和空白输出行,同时保持缩进 [英] XSLT output formatting: removing line breaks, and blank output lines from removed elements while keeping indent

查看:29
本文介绍了XSLT 输出格式:从删除的元素中删除换行符和空白输出行,同时保持缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 XML:

<div><称号>先生标题<段落>这是一个段落.</段落><段落>另一段.</段落><列表><有序列表><项目><paragraph>项目段落.</paragraph></项目><项目><paragraph>另一个项目段落</paragraph></项目></有序列表></list>

</doc>

这是我的 XSL:

<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:模板><xsl:template match="foo:doc"><xsl:element name="newdoc" namespace="http://www/w3.org/1999/xhtml"><xsl:apply-templates/></xsl:element></xsl:模板><xsl:template match="foo:div"><segment title="{foo:title}"><xsl:apply-templates/></段></xsl:模板><xsl:template match="foo:title"><xsl:element name="h2"><xsl:apply-templates/></xsl:element></xsl:模板><xsl:template match="foo:paragraph"><xsl:元素名称="p"><xsl:apply-templates/></xsl:element></xsl:模板><xsl:template match="foo:list"><xsl:apply-templates/></xsl:模板><xsl:template match="foo:orderedlist"><xsl:element name="ol"><xsl:apply-templates/></xsl:element></xsl:模板><xsl:template match="foo:item"><xsl:element name="li"><xsl:apply-templates/></xsl:element></xsl:模板><xsl:template match="foo:item/foo:paragraph"><xsl:apply-templates/></xsl:模板></xsl:stylesheet>

和输出:

<segment xmlns="" title="头衔先生"><h2>先生标题<p>这是一个段落.</p><p>另一段.</p><ol><li>一个项目段落.<li>另一个项目段落</ol></段></newdoc>

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

  1. 从p"元素(最初是段落)中删除换行符
  2. 从li"元素中删除换行符(在删除项目/段落元素时产生)
  3. 删除删除列表项时创建的额外空行

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

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

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

谢谢!!

解决方案

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

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

产生这个输出:

<p>这是一个段落.</p><p>另一段.</p><ol><li>一个项目段落.</li><li>另一个项目段落</li></ol></段></newdoc>

Here is my 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:

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

  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

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

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

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

Thank you!!

解决方案

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>

这篇关于XSLT 输出格式:从删除的元素中删除换行符和空白输出行,同时保持缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆