【问题标题】:unordered list xsl - bullet not showed无序列表 xsl - 项目符号未显示
【发布时间】:2019-12-07 17:44:23
【问题描述】:

我正在创建一个 XSL 模板以通过 XML 显示项目符号列表,但未显示项目符号 这是我的 XSL

<xsl:template match="unorderedList">
    <ul>
        <xsl:for-each select="listitem">
            <li>
                <xsl:apply-templates select="./para"/>
            </li>
        </xsl:for-each>
    </ul>
</xsl:template>

这是我的 XML

<unorderedList>
    <listitem>
        <para><literal>First item</literal></para>
    </listitem>
    <listitem>
        <para><literal>Second item</literal></para>
    </listitem>
</unorderedList>

【问题讨论】:

  • 您在&lt;/unorderedList 上缺少结束&gt;
  • @ScottieG 在哪里?
  • @PrinceVegeta 在哪里?
  • @CristinaMoretti - 自从我发表评论以来,Sanjay 已经编辑了您的代码。
  • 您展示的 XML 和 XSLT sn-p 应该“工作”。见xsltfiddle.liberty-development.net/94rmq7d。你能说一下你尝试的结果是什么吗?如果您显示更多的 XSLT 可能会有所帮助,因为可能是其他原因导致模板不匹配。还要确保您问题中的 XML 真正代表您正在使用的 XML。谢谢!

标签: html xml xslt


【解决方案1】:

我建议不要使用for-each,而是熟悉匹配模板来处理整个结构。

您当前的方法错过了包含您希望呈现为&lt;li&gt; 内容的文本的&lt;literal&gt; 元素。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <!-- usually start out with matching the document root -->
    <xsl:template match="/">
        <!-- ... and pass on to the next matching template -->
        <xsl:apply-templates />
    </xsl:template>

    <!-- your matching ul element -->
    <xsl:template match="unorderedList">
        <ul>
            <!-- ... pass on to the list content -->
            <xsl:apply-templates />
        </ul>
     </xsl:template>

     <!-- nothing to do at this level -->
     <xsl:template match="listitem">
         <!-- ... pass on -->
         <xsl:apply-templates />
     </xsl:template>

     <!-- nothing to do at this level -->
     <xsl:template match="para">
         <!-- ... pass on -->
         <xsl:apply-templates />
     </xsl:template>

     <!-- we reached the li content! --->
     <xsl:template match="literal">
         <li>
             <xsl:value-of select="./text()"/>
         </li>
     </xsl:template>

</xsl:stylesheet>

【讨论】:

  • 这解决了什么问题?大部分添加的模板都是多余的。
  • 我的意图是(如前所述)显示匹配的模板。当然,这非常冗长,但 OP 显然是一个学习者,所以试图展示整个流程流程并没有什么坏处。
  • 我认为目的是解决问题中提出的问题。
  • 正如 Tim C 在他的评论中已经指出的那样,没有问题 - 至少在所示示例中没有。我仍然认为这个答案可以帮助修复 OP 的实际代码,并可能解决此处未显示的真正问题。
  • "没有问题" 没错。这使您的答案不仅无用,而且具有误导性。
猜你喜欢
  • 2015-07-10
  • 2010-09-07
  • 2012-04-20
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 2014-08-14
相关资源
最近更新 更多