2016-05-12 38 views
1

我想用XLST 1.0,以保持低於輸入的層次結構,但改變秩序,使termGrp節點,其descrip節點具有屬性@type=Bevorzugt來在所有其他之前。如何通過複製具有特定屬性的元素首先在XSLT更改訂單1.0

在我的預期輸出中,此更改可看作術語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

在您的XML中沒有包含子元素的'term'元素。 –

+0

謝謝你指出這個錯誤,我誤解了我的文件的結構,並錯誤地使用了這個術語。我現在編輯了我原來的文章,希望現在能夠更清晰地表達我想達到的目標 - 對不起。 – user2564684

+0

你想要的輸出是什麼樣的? – zx485

回答

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節點之前的新位置。

+0

感謝您提供有效的解決方案,並指出潛在問題,如果有'termGrp'之後有節點。在我的嘗試中,我在第二個模板中匹配了'termGrp',而不是'languageGrp'。 – user2564684

相關問題