【问题标题】:XML / XSL Paragraph Elements Nesting IssueXML / XSL 段落元素嵌套问题
【发布时间】:2021-01-03 15:34:44
【问题描述】:

我正在尝试使用此 XML 和 XSL 实现以下输出,尽管我的 XSL 无效,因为段落元素没有正确嵌套。我该如何克服这个问题?

XML

<Phrases>
    <Phrase>
        <FullText>Lorem ipsum dolor sit amet.</FullText>
    </Phrase>
    <Phrase>
        <FullText>Consectetur adipiscing elit.</FullText>
        <NewLine/>
    </Phrase>
    <Phrase>
        <FullText>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</FullText>
    </Phrase>
    <Phrase>
        <FullText>Ut enim ad minim veniam.</FullText>
    </Phrase>
</Phrases>

XSL

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:call-template name="Phrase">
        <xsl:with-param name="Phrases" select="Phrases"/>
    </xsl:call-template>

    <!-- Phrase -->
    <xsl:template name="Phrase">
        <xsl:param name="Phrases"/>
        <p>
            <xsl:for-each select="$Phrases">
                <xsl:value-of select="FullText"/>
                <xsl:if test="position() != last() and not(NewLine)">
                    &#160;
                </xsl:if>
                <xsl:if test="position() != last() and NewLine">
                    </p><p>
                </xsl:if>
            </xsl:for-each>
        </p>
    </xsl:template>

</xsl:stylesheet>

预期输出

<p>Lorem ipsum dolor sit amet.&#160;Consectetur adipiscing elit.</p>
<p>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.&#160;Ut enim ad minim veniam.</p>

【问题讨论】:

  • 您使用哪个 XSLT 处理器,哪个 XSLT 版本?

标签: html xml xslt element paragraph


【解决方案1】:

使用 XSLT 2 或 3 你可以使用

  <xsl:template match="Phrases">
      <xsl:for-each-group select="Phrase" group-ending-with="Phrase[NewLine]">
          <p>
              <xsl:value-of select="current-group()/FullText" separator="&#160;"/>
          </p>
      </xsl:for-each-group>
  </xsl:template>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-26
    • 2014-11-20
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多