我想使用XLST 1.0来维护下面输入的层次结构,但要更改顺序,使其descrip
节点具有属性@type=Bevorzugt
的所有termGrp
节点位于所有其他节点之前。
在我的预期输出中,这个变化可以被视为术语Immatrikulationsfrist
,并且在第一个输入文件Einschreibefrist
(原文如此)中被排序在Einschreibefrist
之前。
我认为正确的方法是首先使用标识模板,但我无法确定如何在之后进行排序,以确保首先处理具有特定属性的元素。我看过<xsl:sort>
,但我只是在学习,我不知道如何处理它,我真的很感激一些建议。
输入
<?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>
尝试以下操作:
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
节点之前的新位置。