XSLT 输出格式:删除换行符,并从删除的元素中删除输出行,同时保持缩进
这是我的 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>
这是我的 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>
我想更改有关此输出的 3 件事:
- 从“p”元素(原始段落)中删除换行符
- 从“li”元素中删除换行符(在 item/paragraph 时生成)元素被删除)
- 删除删除列表项时创建的额外空白行
- 我尝试过
对于 #3,但这与缩进混淆
- 我也尝试过
对于 #1,但这对换行符没有影响
- 我已经尝试过
但是这消除了所有缩进
谢谢!!
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:
- remove the line break from the "p" elements (originally paragraph)
- remove the line breaks from the "li" elements (produced when item/paragraph elements were removed)
- 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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将这些模板添加到样式表中:
产生以下输出:
带有
match="*/text()[normalize-space()]"
的模板将匹配text()
节点如果从normalize-space()
有一定的价值。来自全空白
text()
的空字符串将计算为false()
并且不匹配。另一个模板匹配相反的条件,并且由于它是空模板,因此将从输出中消除仅空白的text()
。Adding these templates to your stylesheet:
Produces this output:
The template with
match="*/text()[normalize-space()]"
will matchtext()
nodes if the string returned fromnormalize-space()
has some value. An empty string from an all whites-spacetext()
would evaluate tofalse()
and not be matched. The other template matches the opposite condition, and since it is an empty template, will eliminate the white-space onlytext()
from the output.在样式表的最后添加这两个模板:
您现在就得到了想要的结果:
At the very end of the stylesheet add these two templates:
You now get the wanted result: