【问题标题】:Line Break XSL doesn't work换行符 XSL 不起作用
【发布时间】: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>

我不明白为什么我的&lt;BR&gt; 在文本末尾。

【问题讨论】:

  • Fabrice,您的 XML 有问题 - &lt;unittitle label="some text1 &lt;lb /&gt;some text2&lt;lb /&gt;some text3.&lt;/unittitle&gt; 格式不正确。如果您解决此问题并显示使用您的其他模板(尤其是 unittitle 的模板),我们可能会有所帮助:)
  • 那个模板很好,你还需要展示其他模板,他们需要使用&lt;xsl:apply-templates/&gt;来确保像lb元素这样的子节点被处理并且按照文档顺序处理.
  • Nic,是的,在 Stack 中,我犯了一个错误。它现在已在我的帖子中修复。 Martin,我的 XSL 有 1470 行。不过,我不能在这里全部粘贴?
  • 好吧,隔离问题,然后发布上下文,我们需要查看unittitledid 元素的任何模板。基本上,如果您使用&lt;xsl:template match="unittitle"&gt;&lt;td&gt;&lt;xsl:apply-templates/&gt;&lt;/td&gt;&lt;/xsl:template&gt; 之类的方法,则输入顺序应保留在输出顺序中,这意味着br 元素出现在lb 元素的位置。
  • 我在我的帖子中添加了来自我的 XSL 的更多详细信息。 ;)

标签: html xml xslt line-breaks


【解决方案1】:

由于我们无法访问您的整个 XSLT,因此很难为您提供最干净的建议,但是在文本之后输出您的 lbs 的原因是因为您在申请之前输出了 text()模板到子节点!

请试一试:

<!--  le contenu d'un noeud -->
<xsl:template name="_noeud">
    <xsl:param name="noeud"/>
    <xsl:for-each select="$noeud">
        <xsl:text> </xsl:text>
        <xsl:apply-templates />
        <xsl:text> </xsl:text>
        <xsl:if test="$avec_label and @label">
            <span class='ead_label'>[<xsl:value-of select="@label"/>]</span>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

【讨论】:

  • 您可以在这里观看我的 XSL 和 XML:jsfiddle.net/Bd9Au/1 抱歉,我不知道所有网站都可以为您发布此代码。
  • @user3766698 恐怕我无法为您挖掘这么大的 XSLT,但上述方法有效吗?
【解决方案2】:

改变

<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 name="cree_cote">
    <td align='left' valign='top' class="titres">
        <xsl:apply-templates select="did/unittitle"/>
    </td>
</xsl:template>

然后改变

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

<xsl:template match="did/unittitle">
  <xsl:text> </xsl:text>
  <xsl:apply-templates/>
  <xsl:text> </xsl:text>
  <xsl:if test="$avec_label and @label">
    <span class="ead_label">[xsl:value-of select="@label"/>]</span>
  </xsl:if>
</xsl:template>

当然,还要保留lb 元素的模板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多