如何通过在XSLT 1.0中复制具有特定属性的元素来更改顺序

时间:2016-05-12 14:54:11

标签: xslt xslt-1.0

我想使用XLST 1.0维护下面输入的层次结构,但更改顺序,以便termGrp个节点具有属性descrip的所有@type=Bevorzugt个节点都在此之前其他

在我的预期输出中,此更改可被视为术语Immatrikulationsfrist,并在输入文件Einschreibefrist(原文如此)中的Einschreibefrist之前排序。

我认为正确的方法是使用身份模板开始,但我无法弄清楚如何排序,以确保首先处理具有特定属性的元素。我看过<xsl:sort>,但我只是在学习,我不知道如何处理这个问题,并且非常感谢一些指示。

INPUT

<?xml version="1.0" encoding="utf-8" ?>
<mtf>
    <conceptGrp>
        <concept>371</concept>
        <transacGrp>
            <transac type="origination">
                LookUp Admin
            </transac>
            <date>
                2016-05-12T10:12:39
            </date>
        </transacGrp>
        <transacGrp>
            <transac type="modification">
                LookUp Admin
            </transac>
            <date>
                2016-05-12T10:12:39
            </date>
        </transacGrp>
        <descripGrp>
            <descrip type="Definition">The timespan during which you can enrol in a degree programme.</descrip>
        </descripGrp>
        <descripGrp>
            <descrip type="Status">freigegeben</descrip>
        </descripGrp>
        <languageGrp>
            <language lang="DE" type="German" />
            <descripGrp>
                <descrip type="Context">Nach Ablauf der Immatrikulationsfrist ist es nicht mehr möglich, sich für einen Studiengang einzuschreiben.</descrip>
            </descripGrp>
            <termGrp>
                <term>Einschreibefrist</term>
                <descripGrp>
                    <descrip type="Segmentation">Ein-schrei-be-frist</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Part of Speech">Noun</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Gender">f.</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Term Type">Variant</descrip>
                </descripGrp>
                <transacGrp>
                    <transac type="origination">
                        LookUp Admin
                    </transac>
                </transacGrp>
                <transacGrp>
                    <transac type="modification">
                        LookUp Admin
                    </transac>
                </transacGrp>
            </termGrp>
            <termGrp>
                <term>Immatrikulationsfrist</term>
                <descripGrp>
                    <descrip type="Segmentation">Im-ma-tri-ku-la-ti-ons-frist</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Part of Speech">Noun</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Gender">f.</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Term Type">fullForm</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Bevorzugt">1</descrip>
                </descripGrp>
                <transacGrp>
                    <transac type="origination">
                        LookUp Admin
                    </transac>
                </transacGrp>
                <transacGrp>
                    <transac type="modification">
                        LookUp Admin
                    </transac>
                </transacGrp>
            </termGrp>
        </languageGrp>
        <languageGrp>
            <language lang="EN" type="English" />
            <descripGrp>
                <descrip type="Context">Please make sure to transfer the semester fee before the enrolment deadline.</descrip>
            </descripGrp>
            <termGrp>
                <term>enrollment deadline</term>
                <descripGrp>
                    <descrip type="Segmentation">en-roll-ment dead-line</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Geographical Usage">AmE</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Part of Speech">Noun</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Term Type">fullForm</descrip>
                </descripGrp>
                <transacGrp>
                    <transac type="origination">
                        LookUp Admin
                    </transac>
                </transacGrp>
                <transacGrp>
                    <transac type="modification">
                        LookUp Admin
                    </transac>
                </transacGrp>
            </termGrp>
            <termGrp>
                <term>enrolment deadline</term>
                <descripGrp>
                    <descrip type="Segmentation">en-rol-ment dead-line</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Geographical Usage">BrE</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Part of Speech">Noun</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Term Type">fullForm</descrip>
                </descripGrp>
                <transacGrp>
                    <transac type="origination">
                        LookUp Admin
                    </transac>
                </transacGrp>
                <transacGrp>
                    <transac type="modification">
                        LookUp Admin
                    </transac>
                </transacGrp>
            </termGrp>
        </languageGrp>
    </conceptGrp>
</mtf>

预期输出

<?xml version="1.0" encoding="utf-8" ?>

<mtf>
    <conceptGrp>
        <concept>371</concept>
        <transacGrp>
            <transac type="origination">
                LookUp Admin
            </transac>
            <date>
                2016-05-12T10:12:39
            </date>
        </transacGrp>
        <transacGrp>
            <transac type="modification">
                LookUp Admin
            </transac>
            <date>
                2016-05-12T10:12:39
            </date>
        </transacGrp>
        <descripGrp>
            <descrip type="Definition">The timespan during which you can enrol in a degree programme.</descrip>
        </descripGrp>
        <descripGrp>
            <descrip type="Status">freigegeben</descrip>
        </descripGrp>
        <languageGrp>
            <language lang="DE" type="German" />
            <descripGrp>
                <descrip type="Context">Nach Ablauf der Immatrikulationsfrist ist es nicht mehr möglich, sich für einen Studiengang einzuschreiben.</descrip>
            </descripGrp>
            <termGrp>
                <term>Immatrikulationsfrist</term>
                <descripGrp>
                    <descrip type="Segmentation">Im-ma-tri-ku-la-ti-ons-frist</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Part of Speech">Noun</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Gender">f.</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Term Type">fullForm</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Bevorzugt">1</descrip>
                </descripGrp>
                <transacGrp>
                    <transac type="origination">
                        LookUp Admin
                    </transac>
                </transacGrp>
                <transacGrp>
                    <transac type="modification">
                        LookUp Admin
                    </transac>
                </transacGrp>
            </termGrp>

            <termGrp>
                <term>Einschreibefrist</term>
                <descripGrp>
                    <descrip type="Segmentation">Ein-schrei-be-frist</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Part of Speech">Noun</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Gender">f.</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Term Type">Variant</descrip>
                </descripGrp>
                <transacGrp>
                    <transac type="origination">
                        LookUp Admin
                    </transac>
                </transacGrp>
                <transacGrp>
                    <transac type="modification">
                        LookUp Admin
                    </transac>
                </transacGrp>
            </termGrp>

        </languageGrp>
        <languageGrp>
            <language lang="EN" type="English" />
            <descripGrp>
                <descrip type="Context">Please make sure to transfer the semester fee before the enrolment deadline.</descrip>
            </descripGrp>
            <termGrp>
                <term>enrollment deadline</term>
                <descripGrp>
                    <descrip type="Segmentation">en-roll-ment dead-line</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Geographical Usage">AmE</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Part of Speech">Noun</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Term Type">fullForm</descrip>
                </descripGrp>
                <transacGrp>
                    <transac type="origination">
                        LookUp Admin
                    </transac>
                </transacGrp>
                <transacGrp>
                    <transac type="modification">
                        LookUp Admin
                    </transac>
                </transacGrp>
            </termGrp>
            <termGrp>
                <term>enrolment deadline</term>
                <descripGrp>
                    <descrip type="Segmentation">en-rol-ment dead-line</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Geographical Usage">BrE</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Part of Speech">Noun</descrip>
                </descripGrp>
                <descripGrp>
                    <descrip type="Term Type">fullForm</descrip>
                </descripGrp>
                <transacGrp>
                    <transac type="origination">
                        LookUp Admin
                    </transac>
                </transacGrp>
                <transacGrp>
                    <transac type="modification">
                        LookUp Admin
                    </transac>
                </transacGrp>
            </termGrp>
        </languageGrp>
    </conceptGrp>
</mtf>

1 个答案:

答案 0 :(得分:1)

尝试类似:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="languageGrp">
    <xsl:copy>
        <xsl:apply-templates select="*[not(self::termGrp)]"/>
        <xsl:apply-templates select="termGrp[descripGrp/descrip/@type='Bevorzugt']"/>
        <xsl:apply-templates select="termGrp[not(descripGrp/descrip/@type='Bevorzugt')]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

请注意,这假设在languageGrp内没有termGrp之后的节点 - 否则这些节点将被移动到termGrp节点之前的新位置。