【发布时间】:2014-06-23 08:56:44
【问题描述】:
我有一个这样的 XML:
<c id="de-4" level="file">
<did>
<unitid label="Cotes">1 DNG 5</unitid>
<unittitle label="my title">some text1 <lb />some text2<lb />some text3.</unittitle>
<unitdate label="Date" normal="2014">2014</unitdate>
</did>
我想使用和转换 LB 标记 -> BR 为 HTML。 我使用 XSL 样式表来执行此操作:
<xsl:template name="cree_cote">
<td align='left' valign='top' class="titres">
<xsl:call-template name="_noeud">
<xsl:with-param name="noeud" select="did/unittitle"/>
</xsl:call-template>
</td>
</xsl:template>
<xsl:template match="lb">
<br />
</xsl:template>
<!-- le contenu d'un noeud -->
<xsl:template name="_noeud">
<xsl:param name="noeud"/>
<xsl:for-each select="$noeud">
<xsl:text> </xsl:text>
<xsl:value-of select="text()"/>
<xsl:text> </xsl:text>
<xsl:if test="$avec_label">
<xsl:if test="@label">
<span class='ead_label'>[<xsl:value-of select="@label"/>]</span>
</xsl:if>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates select="$noeud/*"/>
</xsl:template>
但转换后,我有这个:
<td valign="top" align="left" class="titres">some text1 some text2some text3.<br><br></td>
我不明白为什么我的<BR> 在文本末尾。
【问题讨论】:
-
Fabrice,您的 XML 有问题 -
<unittitle label="some text1 <lb />some text2<lb />some text3.</unittitle>格式不正确。如果您解决此问题并显示使用您的其他模板(尤其是unittitle的模板),我们可能会有所帮助:) -
那个模板很好,你还需要展示其他模板,他们需要使用
<xsl:apply-templates/>来确保像lb元素这样的子节点被处理并且按照文档顺序处理. -
Nic,是的,在 Stack 中,我犯了一个错误。它现在已在我的帖子中修复。 Martin,我的 XSL 有 1470 行。不过,我不能在这里全部粘贴?
-
好吧,隔离问题,然后发布上下文,我们需要查看
unittitle和did元素的任何模板。基本上,如果您使用<xsl:template match="unittitle"><td><xsl:apply-templates/></td></xsl:template>之类的方法,则输入顺序应保留在输出顺序中,这意味着br元素出现在lb元素的位置。 -
我在我的帖子中添加了来自我的 XSL 的更多详细信息。 ;)
标签: html xml xslt line-breaks