xslt - XSLT 转换文件中产生的额外换行符
问题描述
我使用 XSLT 将文档从 XML 转换为文本。但是每次实例后都会产生额外的行空间,请建议如何解决这个问题。在这里,我附上了屏幕截图供您参考。
例子
输入:
<?xml version="1.0" encoding="UTF-8"?>
<book-part book-part-type="chapter" id="chapter1">
<book-part-meta>
<title-group>
<label>1</label>
<title>The Developmental Origins of Health and Disease—Where Did It All Begin?</title>
</title-group>
<contrib-group>
<contrib contrib-type="author"><name><surname>Nicholas</surname><given-names>L. M.</given-names></name></contrib>
<contrib contrib-type="author"><name><surname>Ozanne</surname><given-names>S. E.</given-names></name></contrib>
</contrib-group>
</book-part-meta>
<body>
<sec id="sec1_1">
<label>1.1</label>
<title>THE DEVELOPMENTAL ORIGINS OF ADULT DISEASE—ORIGINS OF THE HYPOTHESIS</title>
<p>One of the earliest proposals establishing the association between early life events and the risk for disease in adult life was more than 80 years ago by Kermack and colleagues.
<fig id="fig1_1">
<label>FIGURE 1.1</label>
<caption><p>Exposure to suboptimal nutrition during fetal development results in an adaptive response to optimize the growth of key body organs to the detriment of others. </p></caption>
<graphic href="001x001"/>
</fig>
</p>
</sec>
</body>
</book-part>
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:output method="text" omit-xml-declaration="yes" standalone="yes" indent="no"/>
[enter image description here][1]
<xsl:template match="fig/label"/>
<xsl:template match="fig/caption">
<xsl:text disable-output-escaping="yes">\caption{</xsl:text><xsl:apply-templates/><xsl:text disable-output-escaping="yes">}</xsl:text>
</xsl:template>
<xsl:template match="fig/graphic">
<xsl:text disable-output-escaping="yes">\includegraphics{</xsl:text><xsl:apply-templates select="@href"/><xsl:text disable-output-escaping="yes">.pdf}</xsl:text>
</xsl:template>
</xsl:stylesheet>
解决方案
您可以使用<xsl:strip-space elements="*" />
删除额外的换行符。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:output method="text" omit-xml-declaration="yes" standalone="yes" indent="no" />
<xsl:strip-space elements="*" />
<xsl:template match="fig/label" />
<xsl:template match="fig/caption">
<xsl:text disable-output-escaping="yes">\caption{</xsl:text>
<xsl:apply-templates />
<xsl:text disable-output-escaping="yes">}</xsl:text>
</xsl:template>
<xsl:template match="fig/graphic">
<xsl:text disable-output-escaping="yes">\includegraphics{</xsl:text>
<xsl:apply-templates select="@href" />
<xsl:text disable-output-escaping="yes">.pdf}</xsl:text>
</xsl:template>
</xsl:stylesheet>
http://xsltransform.net/gVAjbT2
该<xsl:strip-space>
元素用于定义应删除空白的元素。保留空白是默认设置,因此只有在使用该元素时才需要使用该<xsl:strip-space>
元素。
推荐阅读
- android - 如何从 Firebase 数据库中检索特定数据
- r - 抓取维基媒体类别树
- sql - 如果从 sql server 中的 sql 查询中找不到任何行,如何获取 0
- java - 退出后,会话没有结束,你重新登录
- c# - 进入插入查询:索引(从零开始)必须大于或等于零且小于参数列表的大小
- python - 很少替换的 Python 性能
- reactjs - 在 ReactJS 中的组件之间传递非常量值(图像 url)
- c++ - 将 std::ostringstream 转换为 std::stringstream
- python - showinfo 和 showwarning 出现在 tkinter.messagebox 的背景中
- r - R中的正态分布(平均值和标准差的值是多少?)