组相邻节点

时间:2013-12-26 06:20:07

标签: xslt-2.0

我想使用< disp-quote> 跟随兄弟< attrib> 对相邻节点< speech> 进行分组。下面是示例XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<body>
<sec id="s1">
    <title>Introduction</title>
    <para>Sample text here</para>
    <sec id="s1.1">
        <title>Title</title>
        <para>Blah blah blah</para>
        <speech>
            <speaker>PHRYNIA</speaker>
            <para>sample speech</para>
        </speech>
        <speech>
            <speaker>PHRYNIA</speaker>
            <para>sample speech</para>
        </speech>
        <speech>
            <speaker>JOEL</speaker>
            <para>sample reply</para>
        </speech>
        <attrib>From eternity...</attrib>
        <para>Blah blah blah 2</para>
        <speech>
            <speaker>PHRYNIA</speaker>
            <para>sample speech</para>
        </speech>
        <test>zzzz</test>
        <speech>
            <speaker>JOEL</speaker>
            <para>sample reply</para>
        </speech>
        <para>Blah blah blah 3</para>
    </sec>
    <sec id="sec1.22">
        <title>aaa</title>
        <para>Text</para>
        <speech><speaker>Facilitator:</speaker><para>Have you been in <bold>love</bold>?</para></speech>
        <speech><speaker>Interviewer:</speaker><para>Yes <italic>I</italic> have been</para></speech>
        <speech><speaker>Facilitator:</speaker><para>Honesto, yung totoo?</para></speech>
        <attrib>Information source</attrib>
    </sec>
</body>

如果我使用以下模板:

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


<!-- XSLT Template to copy anything, priority="-1" -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="//sec">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:for-each-group select="*"
            group-adjacent="self::speech or (self::attrib and preceding-sibling::*[1][self::speech])">
            <xsl:choose>
                <xsl:when test="current-grouping-key() and current-group()[2]">
                    <disp-quote>
                        <xsl:copy-of select="current-group()"/>
                    </disp-quote>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="current-group()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

什么都没发生。样式表未对语音 attrib 节点进行分组。我认为这是因为嵌套的&lt; sec&gt; 元素。我已删除

<sec id="s1">
<title>Introduction</title>
<para>Sample text here</para>

和结束标记

</sec>

它产生了正确的结果。正确的输出应该是(嵌套&lt; sec&gt; ):

<?xml version="1.0" encoding="UTF-8"?>
<body>
<sec id="s1">
    <title>Introduction</title>
    <para>Sample text here</para>
    <sec id="s1.1">
        <title>Title</title>
        <para>Blah blah blah</para>
        <disp-quote>
            <speech>
                <speaker>PHRYNIA</speaker>
                <para>sample speech</para>
            </speech>
            <speech>
                <speaker>PHRYNIA</speaker>
                <para>sample speech</para>
            </speech>
            <speech>
                <speaker>JOEL</speaker>
                <para>sample reply</para>
            </speech>
            <attrib>From eternity...</attrib>
        </disp-quote>
        <para>Blah blah blah 2</para>
        <speech>
            <speaker>PHRYNIA</speaker>
            <para>sample speech</para>
        </speech>
        <test>zzzz</test>
        <speech>
            <speaker>JOEL</speaker>
            <para>sample reply</para>
        </speech>
        <para>Blah blah blah 3</para>
    </sec>
    <sec id="sec1.22">
        <title>aaa</title>
        <para>Text</para>
        <disp-quote>
            <speech>
                <speaker>Facilitator:</speaker>
                <para>Have you been in <bold>love</bold>?</para>
            </speech>
            <speech>
                <speaker>Interviewer:</speaker>
                <para>Yes <italic>I</italic> have been</para>
            </speech>
            <speech>
                <speaker>Facilitator:</speaker>
                <para>Honesto, yung totoo?</para>
            </speech>
            <attrib>Information source</attrib>
        </disp-quote>
    </sec>
</body>

这里的想法是将相邻的语音节点与&lt; disp-quote&gt; 标记中的以下兄弟 attrib 分组并保留其余部分原样。

非常感谢提前。

1 个答案:

答案 0 :(得分:0)

而不是两个<xsl:copy-of select="current-group()"/>使用<xsl:apply-templates select="current-group()"/>