xml - 我可以使用 XSLT 1.0 在 XML 元素的文本内容周围添加 <p></p> 标签吗?

标签 xml xslt xslt-1.0

我目前正在整理一个动态 HTML 模板,该模板由 XML 数据和 XSLT (1.0) 的简单文档组合提供便利,但我最近遇到了一个问题,我希望得到一些建议上。

在我的 XML 和 XSLT 文档之间建立连接后,我一直在使用标准 <xsl:value-of select="xxxxxx/xxxxxx"/>命令绘制出包含在我的 XML 元素中的文本节点并将这些节点输出到 HTML 模板。太棒了!

但是,我在这个过程中遇到了问题。

我的一个 XML 元素(在此示例中为 <synopsis></synopsis>)包含大量文本,目前由换行符分隔,理想情况下我需要在我的 HTML 页面上呈现为(三个单独的)段落。

让我与您分享XML数据示例:

<bookstore>
  <book>
    <title>Harry Potter and the Philosopher's Stone</title>
    <author>J K. Rowling</author>
    <year>1997</year>
    <price>3.99</price>
    <publisher>Bloomsbury (UK)</publisher>
    <synopsis>
         Harry Potter and the Philosopher's Stone is the first novel in the Harry Potter series and J. K. Rowling's debut novel. 

         The plot follows Harry Potter, a young wizard who discovers his magical heritage as he makes close friends and a few enemies in his first year at the Hogwarts School of Witchcraft and Wizardry. 

         With the help of his friends, Harry faces an attempted comeback by the dark wizard Lord Voldemort, who killed Harry's parents, but failed to kill Harry when he was just a year old.
   </synopsis>
 </book>
 <book>
    <title>The Girl with the Dragon Tattoo</title>
    <author>Stieg Larsson</author>
    <year>2005</year>
    <price>5.99</price>
    <publisher>Norstedts Förlag (SWE)</publisher>
    <synopsis>
         In Stockholm, Sweden, journalist Mikael Blomkvist, co-owner of Millennium magazine, has lost a libel case brought against him by businessman Hans-Erik Wennerström. Lisbeth Salander, a brilliant but troubled investigator and hacker, compiles an extensive background check on Blomkvist for business magnate Henrik Vanger, who has a special task for him. 

         In exchange for the promise of damning information about Wennerström, Blomkvist agrees to investigate the disappearance and assumed murder of Henrik's grandniece, Harriet, 40 years ago. 

         After moving to the Vanger family's compound, Blomkvist uncovers a notebook containing a list of names and numbers that no one has been able to decipher.
    </synopsis>
  </book>
</bookstore>

使用 <xsl:value-of select="xxxxxx/xxxxxx"/> 时命令,我当前的输出为 <synopsis></synopsis> elements 显然是一个没有空格的连续文本 block (在三个段落之间); 哈利波特示例:

    Harry Potter and the Philosopher's Stone is the first novel in the Harry Potter series and J. K. Rowling's debut novel.The plot follows Harry Potter, a young wizard who discovers his magical heritage as he makes close friends and a few enemies in his first year at the Hogwarts School of Witchcraft and Wizardry.With the help of his friends, Harry faces an attempted comeback by the dark wizard Lord Voldemort, who killed Harry's parents, but failed to kill Harry when he was just a year old.

我想在我的 HTML 输出中实现的(希望 XLST 1.0 中的模板能够促进)是:

    <p>Harry Potter and the Philosopher's Stone is the first novel in the Harry Potter series and J. K. Rowling's debut novel.</p>

    <p>The plot follows Harry Potter, a young wizard who discovers his magical heritage as he makes close friends and a few enemies in his first year at the Hogwarts School of Witchcraft and Wizardry.</p>

    <p>With the help of his friends, Harry faces an attempted comeback by the dark wizard Lord Voldemort, who killed Harry's parents, but failed to kill Harry when he was just a year old.</p>

所以这是我的主要问题。可以通过我的 XSLT (1.0) 文件中的某种形式的模板来实现(上述)解决方案吗?这甚至可以做到吗?

我可以设置一个字符串变量(属于 <synopsis> )然后搜索它并找到文本段落中的每个句号/换行符并在这些点处换行 <p></p>围绕这部分字符串选择的标签来形成我的段落?

如果这确实是一个前景,我想在未来的基础上进行构建,并可能添加一些进一步的 HTML 标签,例如 <ol><li><li></ol>等等

任何关于这是否可以实现的帮助或建议都会受到热烈欢迎。

最佳答案

我认为没有必要修改您的原始 XML 输入。您可以简单地使用换行符作为分隔符(并对剩余的空格应用一些规范化):

<xsl:template match="synopsis">
    Synopsis: <span style="color:#38A930">
    <xsl:call-template name="tokenize">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
    </span>
    <br/>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="'&#10;'"/>
        <xsl:variable name="token" select="normalize-space(substring-before(concat($text, $delimiter), $delimiter))" />
        <xsl:if test="$token">
            <p>
                <xsl:value-of select="$token"/>
            </p>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

关于xml - 我可以使用 XSLT 1.0 在 XML 元素的文本内容周围添加 <p></p> 标签吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32651646/

相关文章:

android - 如何在 XML 选择器中使用字体图标 (font-awesome)

xml - 使用 XSL 更改元素中的单个节点内容,同时复制其余内容

javascript - 加载 xsl 时出错 "invalid xpath expression"

xslt - 如何在 XSLT 中使用内联条件(if then else)?

xml - 转换以删除重复项并复制其余部分

c# - 另一个进程异常使用的文件

xml - 如何使用 XSLT 将 XML 文件拆分为多个 XML 文件

java - 为什么 XML 注释不应该包含隐藏命令?

html - 保留元素内的空间并删除元素之间的空格(空行)XSLT、CSS

xml - 是否有库可以在 erlang 中执行 xslt 转换?