【问题标题】:XSL: How to get all textpieces, keep the order and apply certain templates withinXSL:如何获取所有文本,保持顺序并在其中应用某些模板
【发布时间】:2017-07-15 16:24:52
【问题描述】:

以下代码:

<elem1> This is an example text
<elem2> and i want to select all of this
<elem3> and apply a template on elem3 </elem3>
<elem3> so it gets bold </elem3>
exampletext </elem2>
exampletext  </elem1>

输出应如下所示:

This is an example text and i want to select all of this <b>and apply a template on elem3</b><b> so it gets bold </b> exampletext exampletext 

我怎样才能用 xsl 做到这一点? 如果我使用“文本()”或选择“。”在使用 for-each select="*" 遍历每个元素时,我多次获得一些文本。我如何归档我之前写的结果?即使我为每个元素都做了一个模板,我也不知道如何只获取这个节点的文本而不是子节点的文本。而且它也应该保持相同的顺序,所以我想要输入子节点之前的文本部分等等......

这可能是一个愚蠢的问题,但我是新人,我对此感到绝望:/

【问题讨论】:

  • 你的问题表明你想要 HTML 输出,但你已经标记了这个 xsl-fo。你能确认你确实想要在这里输出 HTML 吗?另外,您可以发布您尝试过的 XSLT 吗?修复损坏的 XSLT 通常比从头开始编写它更容易。谢谢!

标签: xml xslt xsl-fo


【解决方案1】:

只需使用 XSLT 的默认递归 processing model。例如,下面的样式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="elem3">
    <b>
        <xsl:apply-templates/>
    </b>
</xsl:template>

</xsl:stylesheet>

将复制所有文本节点(使用built-in template rules)并在elem3 元素中包含的那些节点周围添加&lt;b&gt; 包装器,以返回:

<?xml version="1.0" encoding="UTF-8"?>
 This is an example text
 and i want to select all of this
<b> and apply a template on elem3 </b><b> so it gets bold </b>
exampletext 
exampletext  

如果您愿意,可以添加:

<xsl:template match="text()">
   <xsl:value-of select="normalize-space()" />
</xsl:template>

然后得到:

<?xml version="1.0" encoding="UTF-8"?>
This is an example textand i want to select all of this<b>and apply a template on elem3</b><b>so it gets bold</b>exampletextexampletext

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多