2010-08-05 34 views
2

我正在使用Perl和XSL。我嘗試將<Interval>的值更改爲XML文件中的某個數字。我的XML看起來是這樣的:爲什麼一個元素在轉換後出現兩次?

<?xml version="1.0"?> 
<Config> 
    <Enabled>false</Enabled> 
    <Interval>5</Interval> 
</Config> 

我的XSL是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
     <xsl:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*"/> 
    <xsl:apply-templates/> 
    </xsl:copy> 
</xsl:template> 
    <xsl:template match="/Config/Interval"> 
<xsl:element name="PollingInterval"> 
    <xsl:element name="Interval">77</xsl:element> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

的問題是,我的輸出具有元素<Interval>兩次:

<?xml version="1.0"?> 
<Config> 
    <Enabled>false</Enabled> 
    <Interval><Interval>77</Interval></Interval> 
</Config> 

請幫助。

+0

問得好(+1)。請參閱我的答案以獲得有關該問題的解釋以及完整且簡單的解決方案。 – 2010-08-05 16:13:33

回答

1

的問題是,我的輸出有 元素兩次:

<?xml version="1.0"?> 
<Config> 
    <Enabled>false</Enabled> 
    <Interval><Interval>77</Interval></Interval> 

</Config> 

事實並非如此!

輸出時,這種轉變是在所提供的XML文檔的應用是:

<Config> 
    <Enabled>false</Enabled> 
    <PollingInterval><Interval>77</Interval></PollingInterval> 
</Config> 

如果你想擺脫任何元素,只需刪除相應的<xsl:element>指令。

例如:移除<xsl:element name="PollingInterval">變換變爲:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="/Config/Interval"> 
     <xsl:element name="Interval">77</xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

和從所述加它所提供的XML文檔的結果爲

<Config> 
    <Enabled>false</Enabled> 
    <Interval>77</Interval> 
</Config> 

我建議,以簡化進一步轉換並匹配Interval的文本節點子節點。這可能是最短的和最簡單的解決方案

<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:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Interval/text()"> 
     <xsl:text>77</xsl:text> 
    </xsl:template> 
</xsl:stylesheet> 

當所提供的XML文檔施加:

<Config> 
    <Enabled>false</Enabled> 
    <Interval>5</Interval> 
</Config> 

有用結果產生

<Config> 
    <Enabled>false</Enabled> 
    <Interval>77</Interval> 
</Config> 

在如果你有很多Interval元素並且想要o NLY通過77那麼除了身份規則的唯一模板應該是替換值5

<xsl:template match="Interval/text()[.=5]"> 
    <xsl:text>77</xsl:text> 
</xsl:template> 
+0

Thanks.Very helpful answer。 – Toren 2010-08-05 22:32:01

1

這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="Interval/text()">77</xsl:template> 
</xsl:stylesheet> 

輸出:

<Config> 
    <Enabled>false</Enabled> 
    <Interval>77</Interval> 
</Config> 

注意:如果您發佈的其他輸入樣本和描述的結合,我們可以告訴你如何提取新的號碼。

編輯:更短。

+0

謝謝。尼斯答案。 – Toren 2010-08-05 22:31:05

相關問題