XSLT-中断list-item中有特殊节点的内容

时间:2018-02-27 13:02:22

标签: xml xslt xslt-2.0

这是我的xml。

<doc>
    <list list-type="alpha-lower">
        <list-item>
            <label>*</label>
            <p>text1</p>
            <list list-type="order">
                <list-item>
                    <label>**</label>
                    <p>text2</p>
                </list-item>
                <list-item>
                    <label>***</label>
                    <p>text3</p>
                    <non-normative-note>non-normative-text</non-normative-note> 
                </list-item>
                <list-item>
                    <label>****</label>
                    <p>text4</p>
                </list-item>
                <list-item>
                    <label>*****</label>
                    <p>text5</p>
                </list-item>
                <list-item>
                    <label>******</label>
                    <p>text6</p>
                </list-item>
            </list>
        </list-item>
        <list-item>
            <label>*******</label>
            <p>text7</p>
            <list list-type="order">
                <list-item>
                    <label>********</label>
                    <p>text8</p>
                </list-item>
                <list-item>
                    <label>*********</label>
                    <p>text9</p>
                </list-item>
                <list-item>
                    <label>**********</label>
                    <p>text10</p>
                </list-item>
            </list>
        </list-item>
    </list>
</doc>

如果<non-normative-note>

中有<list-item>个节点,我需要从其父级中断嵌套列表并需要中断列表

这是我的预期输出,

<doc>
    <list list-type="alpha-lower">
        <list-item>
            <label>*</label>
            <p>text1</p>
        </list-item>
    </list>
    <list list-type="order">
        <list-item>
            <label>**</label>
            <p>text2</p>
        </list-item>
        <list-item>
            <label>***</label>
            <p>text3</p>
        </list-item>
    </list>
    <p><non-normative-note>non-normative-text</non-normative-note> </p>
    <list list-type="order">
        <list-item>
            <label>****</label>
            <p>text4</p>
        </list-item>
        <list-item>
            <label>*****</label>
            <p>text5</p>
        </list-item>
        <list-item>
            <label>******</label>
            <p>text6</p>
        </list-item>
    </list> 
    <list list-type="alpha-lower">
        <list-item>
            <label>*******</label>
            <p>text7</p>
        </list-item>
    </list>
    <list list-type="order">
        <list-item>
            <label>********</label>
            <p>text8</p>
        </list-item>
        <list-item>
            <label>*********</label>
            <p>text9</p>
        </list-item>
        <list-item>
            <label>*********</label>
            <p>text10</p>
        </list-item>
    </list> 
</doc>

这是我现在拥有的XSLT,

<xsl:template match="list[descendant::list]">
        <xsl:variable name="type" select="@list-type"/>
        <xsl:for-each select="list-item">
            <list list-type="{$type}">
                <list-item>
                    <xsl:apply-templates select="node()[not(self::list)]"/>
                </list-item>
            </list>
            <xsl:apply-templates select="list"/>
        </xsl:for-each>
    </xsl:template>

这会成功打破嵌套列表,当需要<non-normative-note>时,我需要将其扩展到中断列表。知道我该怎么办?

1 个答案:

答案 0 :(得分:1)

您可以在此xsl:for-each-group使用group-ending-with来启用您的破解....

尝试将这两个模板添加到您的XSLT

<xsl:template match="list">
    <xsl:variable name="type" select="@list-type"/>
    <xsl:for-each-group select="list-item" group-ending-with="*[non-normative-note]">
        <list list-type="{$type}">
            <xsl:apply-templates select="current-group()" />
        </list>
    </xsl:for-each-group>
</xsl:template>

<xsl:template match="list-item[non-normative-note]">
    <xsl:copy>
        <xsl:apply-templates select="@*|node() except non-normative-note" />
    </xsl:copy>
    <p><xsl:copy-of select="non-normative-note" /></p>
</xsl:template>