我想分组元素集?

时间:2017-06-10 12:53:14

标签: xml xslt-2.0

INPUT:我有@type' PT'的未编号元素以下兄弟未编号@type'章'我想把所有章节分成几部分。:

    <contents>
    <unnumbered type="PT">
        <title>Title one</title>
    </unnumbered>
    <unnumbered type="chapter" manid="01"/>
    <unnumbered type="chapter" manid="02"/>
    <unnumbered type="chapter" manid="03"/>
    <unnumbered type="PT">
        <title>Title Two</title>
    </unnumbered>
    <unnumbered type="chapter" manid="04"/>
    <unnumbered type="chapter" manid="05"/>
    <unnumbered type="chapter" manid="06"/>
    <unnumbered type="PT">
        <title>Title Three</title>
    </unnumbered>
    <unnumbered type="chapter" manid="07"/>
    <unnumbered type="chapter" manid="08"/>
    <unnumbered type="chapter" manid="09"/>
    <unnumbered type="chapter" manid="10"/>
    <unnumbered type="PT">
        <title>Title Four</title>
    </unnumbered>
    <unnumbered type="chapter" manid="11"/>
    <unnumbered type="chapter" manid="12"/>
    <unnumbered type="chapter" manid="13"/>
    <unnumbered type="chapter" manid="14"/>
    <unnumbered type="chapter" manid="15"/>
</contents>

每个部分都会重复输出内容:

    <contents>
    <unnumbered type="PT">
        <title>Title one</title>
        <unnumbered>
            <unnumbered type="chapter" manid="01"/>
            <unnumbered type="chapter" manid="02"/>
            <unnumbered type="chapter" manid="03"/>
            <unnumbered type="chapter" manid="04"/>
            <unnumbered type="chapter" manid="05"/>
            <unnumbered type="chapter" manid="06"/>
            <unnumbered type="chapter" manid="07"/>
            <unnumbered type="chapter" manid="08"/>
            <unnumbered type="chapter" manid="09"/>
            <unnumbered type="chapter" manid="10"/>
            <unnumbered type="chapter" manid="11"/>
            <unnumbered type="chapter" manid="12"/>
            <unnumbered type="chapter" manid="13"/>
            <unnumbered type="chapter" manid="14"/>
            <unnumbered type="chapter" manid="15"/>
        </unnumbered>
    </unnumbered>
    <unnumbered type="PT">
        <title>Title Two</title>
        <unnumbered>
            <unnumbered type="chapter" manid="04"/>
            <unnumbered type="chapter" manid="05"/>
            <unnumbered type="chapter" manid="06"/>
            <unnumbered type="chapter" manid="07"/>
            <unnumbered type="chapter" manid="08"/>
            <unnumbered type="chapter" manid="09"/>
            <unnumbered type="chapter" manid="10"/>
            <unnumbered type="chapter" manid="11"/>
            <unnumbered type="chapter" manid="12"/>
            <unnumbered type="chapter" manid="13"/>
            <unnumbered type="chapter" manid="14"/>
            <unnumbered type="chapter" manid="15"/>
        </unnumbered>
    </unnumbered>
    <unnumbered type="PT">
        <title>Title Three</title>
        <unnumbered>
            <unnumbered type="chapter" manid="07"/>
            <unnumbered type="chapter" manid="08"/>
            <unnumbered type="chapter" manid="09"/>
            <unnumbered type="chapter" manid="10"/>
            <unnumbered type="chapter" manid="11"/>
            <unnumbered type="chapter" manid="12"/>
            <unnumbered type="chapter" manid="13"/>
            <unnumbered type="chapter" manid="14"/>
            <unnumbered type="chapter" manid="15"/>
        </unnumbered>
    </unnumbered>
    <unnumbered type="PT">
        <title>Title Four</title>
        <unnumbered>
            <unnumbered type="chapter" manid="11"/>
            <unnumbered type="chapter" manid="12"/>
            <unnumbered type="chapter" manid="13"/>
            <unnumbered type="chapter" manid="14"/>
            <unnumbered type="chapter" manid="15"/>
        </unnumbered>
    </unnumbered>
</contents>

所需的输出:我想使用属性@type = chapter的以下兄弟包装部件信息:

<contents>
    <unnumbered type="PT">
        <title>Title one</title>
        <unnumbered type="chapter" manid="01"/>
        <unnumbered type="chapter" manid="02"/>
        <unnumbered type="chapter" manid="03"/>
    </unnumbered>
    <unnumbered type="PT">
        <title>Title Two</title>
        <unnumbered type="chapter" manid="04"/>
        <unnumbered type="chapter" manid="05"/>
        <unnumbered type="chapter" manid="06"/>
    </unnumbered>
    <unnumbered type="PT">
        <title>Title Three</title>
        <unnumbered type="chapter" manid="07"/>
        <unnumbered type="chapter" manid="08"/>
        <unnumbered type="chapter" manid="09"/>
        <unnumbered type="chapter" manid="10"/>
    </unnumbered>
    <unnumbered type="PT">
        <title>Title Four</title>
        <unnumbered type="chapter" manid="11"/>
        <unnumbered type="chapter" manid="12"/>
        <unnumbered type="chapter" manid="13"/>
        <unnumbered type="chapter" manid="14"/>
        <unnumbered type="chapter" manid="15"/>
    </unnumbered>
</contents>

使用XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="contents">
        <contents>
            <xsl:for-each-group select="unnumbered" group-starting-with="unnumbered[@type='PT']">
                <unnumbered>
                    <xsl:apply-templates select="@*"/>
                    <xsl:apply-templates/>
                    <xsl:choose>
                        <xsl:when test="following-sibling::unnumbered[@type='chapter']">

                                <unnumbered>
                                    <xsl:apply-templates
                                        select="following-sibling::unnumbered[@type='chapter']"/>
                                </unnumbered>

                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:copy-of select="current-group()"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </unnumbered>
            </xsl:for-each-group>
        </contents>
    </xsl:template>


</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

您在样式表中使用了正确的分组方法,但是在for-each-group内部使您的内容过于复杂,只需使用

<xsl:template match="contents">
    <contents>
        <xsl:for-each-group select="unnumbered" group-starting-with="unnumbered[@type='PT']">
            <unnumbered>
                <xsl:apply-templates select="@*, node(), current-group() except ."/>
            </unnumbered>
        </xsl:for-each-group>
    </contents>
</xsl:template>