如何避免在自己的行上打开xml标记

时间:2014-10-06 06:49:40

标签: xslt

我有一个XML结构如下:

<?xml version="1.0" encoding="utf-8"?>
<cl:doc identifier="ISBN" xsi:schemaLocation="http://xml.cengage-learning.com/cendoc-core cendoc.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cl="http://xml.cengage-learning.com/cendoc-core" xmlns:m="http://www.w3.org/1998/Math/MathML">
<cl:chapter identifier="ch01">
<cl:opener identifier="ch06_opn">
<cl:introduction identifier="ch06_int">
<cl:list identifier="tu_1" list-style="Unformatted" item-length="long">
<cl:item identifier="tu_2"><cl:para identifier="ch01_dum_2">Solubility</cl:para></cl:item>
<cl:item identifier="tu_3"><cl:para identifier="ch01_dum_3">Polarity</cl:para></cl:item>
</cl:list></cl:introduction></cl:opener></cl:chapter></cl:doc>

当我使用XSLT将其转换为xml以上时,我得到了以下输出:

<?xml version="1.0" encoding="utf-8"?>
<cl:doc identifier="ISBN" xsi:schemaLocation="http://xml.cengage-learning.com/cendoc-core cendoc.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cl="http://xml.cengage-learning.com/cendoc-core" xmlns:m="http://www.w3.org/1998/Math/MathML"><cl:chapter identifier="ch01">
<cl:opener identifier="ch06_opn">
<cl:introduction identifier="ch06_int"><cl:list identifier="tu_1" list-style="Unformatted" item-length="long">
<cl:item identifier="tu_2"><cl:para identifier="ch01_dum_2">Solubility</cl:para></cl:item>
<cl:item identifier="tu_3"><cl:para identifier="ch01_dum_3">Polarity</cl:para></cl:item></cl:list></cl:introduction></cl:opener></cl:chapter></cl:doc>

此处,开头标记<cl:opener identifier="ch06_opn">单独出现。这导致我在转换后得到空行。

我需要此<cl:opener identifier="ch06_opn">标记必须与其上一行或下一行一起运行。

任何人都可以帮助我如何通过XSLT实现这一目标。

谢谢, 戈帕尔

1 个答案:

答案 0 :(得分:1)

如果没有看到你的XSLT,很难确定,但听起来你的XSLT正在将源中的空白复制到输出中。

防止这种情况的最快方法是放

<xsl:strip-space elements="*"/>

或者

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

这会删除所有空格,但您当然可以更加具体地了解您要移除的空白,例如

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

只删除该开放元素标记后面的空格 - 如果它只是空格,则匹配cl:opener中的第一个文本节点,并且不输出任何内容。