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

时间:2015-09-18 12:06:21

标签: xml xslt xslt-1.0

我目前正在整理一个动态HTML模板,该模板由XML数据和 XSLT(1.0)的简单文档组合促成,但我最近遇到了一个问题,我和# 39;欣赏一些建议。

在我的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>元素的当前输出显然是一个连续的文本块,没有空格(在三段之间); 哈利波特示例:

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

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

2 个答案:

答案 0 :(得分:3)

我认为不需要修改原始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>

答案 1 :(得分:1)

正如michael.hor257k在他的有用评论(上图)中所揭示的那样,这个问题的解决方案围绕着标记化,这是我在使用XSLT之前没遇到的。< / p>

在这篇文章中使用michael.hor257k的基于分隔符的方便解决方案(已接受的答案) - XSLT - Tokenizing template to italicize and bold XML element text - 我能够将我的元素文本输出到单独的段落中。

更新了XML :(注意我想要转换为段落的文本的每个部分周围的星号)

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

XSLT模板:

<xsl:template name="paragraphs">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="'*'"/>
<xsl:choose>
    <xsl:when test="contains($text, $delimiter) and contains(substring-after($text, $delimiter), $delimiter)">
        <xsl:value-of select="substring-before($text, $delimiter)"/>
        <p>
            <xsl:value-of select="substring-before(substring-after($text, $delimiter), $delimiter)"/>
        </p>
        <!-- recursive call -->
        <xsl:call-template name="paragraphs">
            <xsl:with-param name="text" select="substring-after(substring-after($text, $delimiter), $delimiter)"/>
        </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="$text"/>
    </xsl:otherwise>
</xsl:choose>
</xsl:template>

调用XSLT模板:

<xsl:for-each select="/dataroot/bookstore/book">
    <xsl:call-template name="paragraphs">
         <xsl:with-param name="text" select="synopsis"/>
    </xsl:call-template>
</xsl:for-each>

HTML输出:

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

......还有一个快乐的顾客!!

我还制作了这个简化的XML转换来显示段落输出 - http://xsltransform.net/6r5Gh2B

相关问题