如何在XSLT中实现嵌套列表?

时间:2015-10-02 07:14:52

标签: xslt-2.0

我想为每个<p>元素添加<li>标记,但是当它是嵌套列表时会有一种特殊情况。如果有后代<ol>标记,则必须在此<p>标记之前关闭此添加<ol>。虽然发生了,但我无法删除<s>test tag</s><br/>部分重复。

请帮忙!

输入:

<a>
    <ol>
        <li>
            <s>test tag</s><br/>
            <ol>
                <li>list item1</li>
                <li>list item2</li>
                <li>list item3</li>
            </ol>
        </li>
    </ol>
</a>

预期产出:

<a>
    <ol>
        <li>
            <p><s>test tag</s><br/></p>
            <ol>

                    <p><li>list item1</li></p>
                    <p><li>list item1</li></p>
                    <p><li>list item1</li></p>

            </ol>
        </li>
    </ol>
</a>

XSLT代码:

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

<xsl:template match="li[not(descendant::ol)]">
    <li>
        <p>
            <xsl:apply-templates/>
        </p>
    </li>
</xsl:template>

<xsl:template match="li[descendant::ol]">
    <li>
        <p>
            <xsl:apply-templates select="node()[parent::li][following-sibling::ol]"/>
        </p>
    </li>
    <xsl:apply-templates select=""/>
</xsl:template>

<xsl:template match="node()[parent::li][following-sibling::ol]"/>

1 个答案:

答案 0 :(得分:1)

在没有后代ol的情况下,您所需的输出在每个li之外都有一个p元素,而您的样式表在每个ol中放置一个p。我不知道你真正想要的是什么,但我想你知道如何修复它。

如果有后代ol,您只提供了一个可能的示例,并且一个示例不构成规范。但是,对于该示例,以下内容将起作用:

<xsl:template match="li[ol]">
    <li>
        <p>
            <xsl:apply-templates select="node()[not(self::ol)]"/>
        </p>
        <xsl:apply-templates select="ol"/>
    </li>
</xsl:template>

如果这不能解决一般情况下的问题,那是因为你没有说出一般情况下的要求。