2014-11-23 87 views
-1

請幫我用XSLT。需要從一個選擇框中選擇選項並放置到另一個選擇框。 我有:XSLT替換選項

<form id="USE-THIS-ID-IN-XSLT"> 
    <select>id="USE-THIS-ID-IN-XSLT"> 
     <option>This will be removed</option> 
     <option>This will be removed</option> 
     <option>This will be removed</option> 
    </select> 
</form> 

<select id="USE-THIS-ID-ALSO"> 
    <option>This will go up</option> 
    <option>This will go up</option> 
    <option>This will go up</option> 
</select> 

所以結果被

<form id="USE-THIS-ID-IN-XSLT"> 
    <select>id="USE-THIS-ID-IN-XSLT"> 
     <option>This will go up</option> 
     <option>This will go up</option> 
     <option>This will go up</option> 
    </select> 
</form> 

如果可能的話,需要做的是沒有創建單獨的xsl:模板匹配...(需要在當前做)

+0

爲什麼你需要在同一個模板中做? – 2014-11-23 19:21:04

回答

0

您的輸入是無效的,而不是<select>id="USE-THIS-ID-IN-XSLT">它應該是<select id="USE-THIS-ID-IN-XSLT">。用這種校正,下面的XSLT

<?xml version="1.0" encoding="utf-8"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="html" indent="yes" /> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()" /> 
    </xsl:copy> 
    </xsl:template> 
    <xsl:template match="select[@id='USE-THIS-ID-IN-XSLT']"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()" /> 
     <xsl:copy-of select="//select[@id='USE-THIS-ID-ALSO']/*" /> 
    </xsl:copy> 
    </xsl:template> 
    <xsl:template match="select[@id='USE-THIS-ID-ALSO'] | 
     select[@id='USE-THIS-ID-IN-XSLT']/*"/> 
</xsl:stylesheet> 

當施加到例如輸入

<html> 
    <form id="USE-THIS-ID-IN-XSLT"> 
    <select id="USE-THIS-ID-IN-XSLT"> 
    <option>This will be removed</option> 
    <option>This will be removed</option> 
    <option>This will be removed</option> 
    </select> 
</form> 
<select id="USE-THIS-ID-ALSO"> 
    <option>This will go up</option> 
    <option>This will go up</option> 
    <option>This will go up</option> 
</select> 
</html> 

產生輸出

<html> 
<form id="USE-THIS-ID-IN-XSLT"> 
    <select id="USE-THIS-ID-IN-XSLT"> 
    <option>This will go up</option> 
    <option>This will go up</option> 
    <option>This will go up</option> 
    </select> 
</form> 
</html> 

最後空模板相匹配應該從輸出中除去一切。取而代之的是在模板上匹配你想要保留已刪除選項的選項副本的選擇。我不確定的是,你有你的輸入<select id="USE-THIS-ID-ALSO">但不希望在輸出,也許這是在你的問題所需的輸出一個小錯誤。