2012-04-19 88 views
2

我試圖使用XSLT將context-param作爲最後一個兄弟。沒有共同的父元素,所以任務有點困難。XSLT - 追加兄弟

我想追加以下元素:

<context-param> 
    <param-name>miku</param-name> 
    <param-value>kawaii</param-value> 
</context-param> 

作爲最後context-param元素以下(例如,所有context-param元素必須是彼此相鄰,它們不能在任何地方散落XML) XML:

<web-app> 
    <not_interesting_element1/> 
    <not_interesting_element2/> 

    <context-param> 
    <param-name>not_interesting_param_key1</param-name> 
    <param-value>kawaii</param-value> 
    </context-param> 
    <context-param> 
    <param-name>not_interesting_param_key2</param-name> 
    <param-value>kawaii</param-value> 
    </context-param> 
    <context-param> 
    <param-name>parameterThatsGuaranteedToBeHere</param-name> 
    <param-value>someValue</param-value> 
    </context-param> 


    <not_interesting_element3/> 
    <not_interesting_element4/> 
    <!-- ... servlets, ... --> 
</web-app> 

結果應該是這樣的:

<web-app> 
    <not_interesting_element1/> 
    <not_interesting_element2/> 

    <context-param> 
    <param-name>not_interesting_param_key1</param-name> 
    <param-value>kawaii</param-value> 
    </context-param> 
    <context-param> 
    <param-name>not_interesting_param_key2</param-name> 
    <param-value>kawaii</param-value> 
    </context-param> 
    <context-param> 
    <param-name>parameterThatsGuaranteedToBeHere</param-name> 
    <param-value>someValue</param-value> 
    </context-param> 
    <context-param> 
     <param-name>miku</param-name> 
     <param-value>kawaii</param-value> 
    </context-param> 

    <not_interesting_element3/> 
    <not_interesting_element4/> 
    <!-- ... servlets, ... --> 
</web-app> 

我該怎麼做?

+0

追加什麼?請編輯問題並提供XML文檔和特定的兄弟節點,您要在其中添加'context-param'元素。然後通過評論通知我。 – 2012-04-19 11:56:14

+0

請發佈說明問題的輸入和期望輸出XML樣本。 – 2012-04-19 11:56:30

+0

@Dimitre Novatchev:問題已更新。 – woky 2012-04-19 12:11:19

回答

2

這種轉變:

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

<xsl:param name="pElemToAdd"> 
    <context-param> 
     <param-name>miku</param-name> 
     <param-value>kawaii</param-value> 
    </context-param> 
</xsl:param> 

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

<xsl:template match="context-param[last()]"> 
    <xsl:call-template name="identity"/> 
    <xsl:copy-of select="$pElemToAdd"/> 
</xsl:template> 
</xsl:stylesheet> 

當應用於提供的XML文檔:

<web-app> 
    <not_interesting_element1/> 
    <not_interesting_element2/> 
    <context-param> 
     <param-name>not_interesting_param_key1</param-name> 
     <param-value>kawaii</param-value> 
    </context-param> 
    <context-param> 
     <param-name>not_interesting_param_key2</param-name> 
     <param-value>kawaii</param-value> 
    </context-param> 
    <context-param> 
     <param-name>parameterThatsGuaranteedToBeHere</param-name> 
     <param-value>someValue</param-value> 
    </context-param> 
    <not_interesting_element3/> 
    <not_interesting_element4/> 
    <!-- ... servlets, ... --> 
</web-app> 

產生想要的,正確的結果:

<web-app> 
    <not_interesting_element1/> 
    <not_interesting_element2/> 
    <context-param> 
     <param-name>not_interesting_param_key1</param-name> 
     <param-value>kawaii</param-value> 
    </context-param> 
    <context-param> 
     <param-name>not_interesting_param_key2</param-name> 
     <param-value>kawaii</param-value> 
    </context-param> 
    <context-param> 
     <param-name>parameterThatsGuaranteedToBeHere</param-name> 
     <param-value>someValue</param-value> 
    </context-param> 
    <context-param> 
     <param-name>miku</param-name> 
     <param-value>kawaii</param-value> 
    </context-param> 
    <not_interesting_element3/> 
    <not_interesting_element4/><!-- ... servlets, ... --> 
</web-app> 

說明

  1. 身份規則副本的每個節點 「原樣」。

  2. 有一個模板,覆蓋身份模板。此模板與所有父母孩子的context-param元素中的最後一個context-param元素匹配。

  3. 在重寫模板中執行兩個動作;通過對身份規則的調用複製當前節點;然後將要附加的元素複製到輸出中。爲了方便和靈活性,我們假設要附加的元素作爲參數傳遞給變換。

+0

謝謝。這工作如果我追加一個元素。如果我重複執行該操作,則附加的元素也會重寫前一個元素,因此看起來最後一個'context-path'元素被刪除,並添加了兩個新元素。 – woky 2012-04-19 12:29:58

+0

@woky:如果要附加多個節點,則必須指定要在參數中附加的完整節點集。 – 2012-04-19 12:32:49