2014-04-19 65 views
0

我在學習xslt,所以請原諒,如果下面的xml沒有意義。這裏是我的示例xml。XSLT:選擇第一個節點並替換它的文本

<root> 
<note> 
<to> 
    <test>text</test> 
    <test1>ABC</test1> 
</to> 

<to> 
    <test>text</test> 
    <test1>content1</test1> 
</to> 
<to> 
    <test>text</test> 
    <test1>ABC</test1> 
</to> 
<to> 
    <test>text</test> 
    <test1>content1</test1> 
</to> 
</note> 
<nodeabc> 
<to> 
    <test>text</test> 
    <test1>ABC</test1> 
</to> 

</nodeabc> 
</root> 

我試圖改變文本的第一個節點「測試」,當有節點「test1」與文本= content1。

例如 應該是。

<root> 
<note> 
<to> 
    <test>text</test> 
    <test1>ABC</test1> 
</to> 

<to> 
    <test>text</test> 
    <test1>REPLACED</test1> 
</to> 
<to> 
    <test>text</test> 
    <test1>ABC</test1> 
</to> 
<to> 
    <test>text</test> 
    <test1>content1</test1> 
</to> 
</note> 
<nodeabc> 
<to> 
    <test>text</test> 
    <test1>ABC</test1> 
</to> 

</nodeabc> 
</root> 

我試了幾次xslt,但似乎沒有任何工作。它代替了相應的「test1」具有值content1的節點「test」的所有文本的出現。 XSLT

<xsl:template match="to/test[../test1='content1'][1]/text()">REPLACED 
</xsl:template > 

部分請指導一下就可以解決。

在此先感謝。

回答

1

與當前的表達的問題,是它匹配測試元素是元件的第一個子。在你的情況下,你真的想要匹配第一個元素。

試試這個表達

<xsl:template match="to[test1='content1'][1]/test/text()">REPLACED 
</xsl:template> 
相關問題