根据子元素位置拆分祖先

时间:2016-03-02 03:42:02

标签: xslt

我正在尝试根据其后代元素<p>拆分祖先元素<fnr>。这是我输入的xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<label>
    <bodytext searchtype="COMMENTARY">
        <p>
            <text>The object of this amendment was <emph typestyle="it">reserve</emph><fnr
                    fntoken="CONI_VOL3.0012.FN1" fnrtoken="CONI_VOL3.0012.FN1-R">1</fnr> might be
                necessary for their advancement.</text>
        </p>
        <p>
            <text>The object of the amendment, <emph typestyle="it">Champakam </emph> where the
                above decision.<fnr fntoken="CONI_VOL3.0013.FN2" fnrtoken="CONI_VOL3.0013.FN2-R"
                    >2</fnr></text>
        </p>
        <p>
            <text>There is no definition clause.<fnr fntoken="CONI_VOL3.0014.FN3"
                    fnrtoken="CONI_VOL3.0014.FN3-R">3</fnr></text>
        </p>
        <p>
            <text>The object of the amendment was thus explained in the Statement of Objects and
                Reasons:</text>
        </p>
        <p>
            <blockquote>
                <p>
                    <text>&#x0022;It is laid down amplified.&#x0022;</text>
                </p>
            </blockquote>
        </p>
        <p>
            <text>The amendment.<fnr fntoken="CONI_VOL3.0015.FN4" fnrtoken="CONI_VOL3.0015.FN4-R"
                    >4</fnr></text>
        </p>
        <footnotegrp>
            <footnote fntoken="CONI_VOL3.0008.FN1" fnrtokens="CONI_VOL3.0008.FN1-R">
                <fnlabel>1</fnlabel>
                <fnbody>
                    <p>
                        <refpt type="ext" id="CONI_VOL3.0008.FN1"/>
                        <text><?No_ELL_Open type="text/xml"?>Act <?No_ELL_Close type="text/xml"?>,
                            s. 2<?No_ELL_Close type="text/xml"?>.</text>
                    </p>
                </fnbody>
            </footnote>
            <footnote fntoken="CONI_VOL3.0010.FN2" fnrtokens="CONI_VOL3.0010.FN2-R">
                <fnlabel>2</fnlabel>
                <fnbody>
                    <p>
                        <refpt type="ext" id="CONI_VOL3.0010.FN2"/>
                        <text>Ins. s. 2 <?No_ELL_Close type="text/xml"?> &#x0028;w.e.f.
                            21-1-2006&#x0029;.</text>
                    </p>
                </fnbody>
            </footnote>
            <footnote fntoken="CONI_VOL3.0012.FN1" fnrtokens="CONI_VOL3.0012.FN1-R">
                <fnlabel>1</fnlabel>
                <fnbody>
                    <p>
                        <refpt type="ext" id="CONI_VOL3.0012.FN1"/>
                        <text><emph typestyle="it">State</emph>.</text>
                    </p>
                </fnbody>
            </footnote>
            <footnote fntoken="CONI_VOL3.0013.FN2" fnrtokens="CONI_VOL3.0013.FN2-R">
                <fnlabel>2</fnlabel>
                <fnbody>
                    <p>
                        <refpt type="ext" id="CONI_VOL3.0013.FN2"/>
                        <text>this is second.</text>
                    </p>
                </fnbody>
            </footnote>
            <footnote fntoken="CONI_VOL3.0014.FN3" fnrtokens="CONI_VOL3.0014.FN3-R">
                <fnlabel>3</fnlabel>
                <fnbody>
                    <p>
                        <refpt type="ext" id="CONI_VOL3.0014.FN3"/>
                        <text>This is third.</text>
                    </p>
                </fnbody>
            </footnote>
            <footnote fntoken="CONI_VOL3.0015.FN4" fnrtokens="CONI_VOL3.0015.FN4-R">
                <fnlabel>4</fnlabel>
                <fnbody>
                    <p>
                        <refpt type="ext" id="CONI_VOL3.0015.FN4"/>
                        <text>this is fourth.</text>
                    </p>
                </fnbody>
            </footnote>
        </footnotegrp>
    </bodytext>
</label>

我想要的输出xml文件应为:

<?xml version="1.0" encoding="UTF-8"?>
<lavel>
    <bodytext searchtype="COMMENTARY">
        <p>
            <text>The object of this amendment was <emph typestyle="it">reserve</emph><fnr
                    fntoken="CONI_VOL3.0012.FN1" fnrtoken="CONI_VOL3.0012.FN1-R">1</fnr> might be
                necessary for their advancement.</text>
        </p>
        <footnotegrp>
            <footnote fntoken="CONI_VOL3.0008.FN1" fnrtokens="CONI_VOL3.0008.FN1-R">
                <fnlabel>1</fnlabel>
                <fnbody>
                    <p>
                        <refpt type="ext" id="CONI_VOL3.0008.FN1"/>
                        <text><?No_ELL_Open type="text/xml"?>Act <?No_ELL_Close type="text/xml"?>,
                            s. 2<?No_ELL_Close type="text/xml"?>.</text>
                    </p>
                </fnbody>
            </footnote>
        </footnotegrp>
    </bodytext>

    <bodytext searchtype="COMMENTARY">
        <p>
            <text>The object of the amendment, <emph typestyle="it">Champakam </emph> where the
                above decision.<fnr fntoken="CONI_VOL3.0013.FN2" fnrtoken="CONI_VOL3.0013.FN2-R"
                    >2</fnr></text>
        </p>
        <footnotegrp>
            <footnote fntoken="CONI_VOL3.0010.FN2" fnrtokens="CONI_VOL3.0010.FN2-R">
                <fnlabel>2</fnlabel>
                <fnbody>
                    <p>
                        <refpt type="ext" id="CONI_VOL3.0010.FN2"/>
                        <text>Ins. s. 2 <?No_ELL_Close type="text/xml"?> &#x0028;w.e.f.
                            21-1-2006&#x0029;.</text>
                    </p>
                </fnbody>
            </footnote>
        </footnotegrp>
    </bodytext>
    <bodytext searchtype="COMMENTARY">
        <p>
            <text>There is no definition clause.<fnr fntoken="CONI_VOL3.0014.FN3"
                    fnrtoken="CONI_VOL3.0014.FN3-R">3</fnr></text>
        </p>
        <p>
            <text>The object of the amendment was thus explained in the Statement of Objects and
                Reasons:</text>
        </p>
        <p>
            <blockquote>
                <p>
                    <text>&#x0022;It is laid down amplified.&#x0022;</text>
                </p>
            </blockquote>
        </p>
        <footnotegrp>
            <footnote fntoken="CONI_VOL3.0014.FN3" fnrtokens="CONI_VOL3.0014.FN3-R">
                <fnlabel>3</fnlabel>
                <fnbody>
                    <p>
                        <refpt type="ext" id="CONI_VOL3.0014.FN3"/>
                        <text>This is third.</text>
                    </p>
                </fnbody>
            </footnote>
        </footnotegrp>
    </bodytext>
    <bodytext searchtype="COMMENTARY">
        <p>
            <text>The amendment.<fnr fntoken="CONI_VOL3.0015.FN4" fnrtoken="CONI_VOL3.0015.FN4-R"
                    >4</fnr></text>
        </p>
        <footnotegrp>
            <footnote fntoken="CONI_VOL3.0015.FN4" fnrtokens="CONI_VOL3.0015.FN4-R">
                <fnlabel>4</fnlabel>
                <fnbody>
                    <p>
                        <refpt type="ext" id="CONI_VOL3.0015.FN4"/>
                        <text>this is fourth.</text>
                    </p>
                </fnbody>
            </footnote>
        </footnotegrp>
    </bodytext>
</lavel>

我尝试过以下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:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="pGroupSize" select="1"/>

    <xsl:template match="bodytext[@searchtype='COMMENTARY' and count(.//fnr) &gt; 1]">

        <xsl:for-each-group select=".//fnr" group-by="(position()-1) idiv $pGroupSize">
            <bodytext>
                <xsl:apply-templates select="current-group()"/>
            </bodytext>
        </xsl:for-each-group>
    </xsl:template>


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

提前致谢。请帮忙。

1 个答案:

答案 0 :(得分:0)

对于for-each-group group-starting-with来说,它看起来更像是一项任务,并附加了一个基于密钥的交叉引用:

<?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:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="footnote" match="footnote" use="fnlabel"/>

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

    <xsl:template match="bodytext[@searchtype = 'COMMENTARY' and .//fnr]">
        <xsl:for-each-group select="* except footnotegrp" group-starting-with="*[.//fnr]">
            <bodytext searchtype="COMMENTARY">
                <xsl:apply-templates select="current-group()"/>
                <footnotegrp>
                    <xsl:apply-templates select="key('footnote', .//fnr)[1]"/>
                </footnotegrp>
            </bodytext>
        </xsl:for-each-group>
    </xsl:template>

</xsl:stylesheet>

但我不确定交叉引用,您可能需要解释fnr元素与footnote元素的关系,如果上述内容未提供所需的输出。