2017-11-25 262 views
2

所以我有一個條件改造這個XSL如果溫度> 20移除標籤以其他方式複製溫度 到目前爲止,我有這樣的事情XSL刪除標籤,如果溫度> 20以其他方式複製標籤

<?xml version="1.0" ?> 
<message-in> 
    <realised-gps> 
     <id>64172068</id> 
     <resourceId>B-06- KXO</resourceId> 
      <position> 
       <coordinatesystem>Standard</coordinatesystem> 
       <latitude>44.380765</latitude> 
       <longitude>25.9952</longitude> 
      </position> 
     <time>2011-05- 23T10:34:46</time> 
     <temperature>21.01</temperature> 
     <door>0</door> 
    </realised-gps> 
</message-in> 

這只是去除標籤,我不能讓其他方式或其他if條件

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

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

<xsl:template match="temperature"> 
     <xsl:if test="temperature &gt; 20"> 
      <xsl:apply-templates /> 
     </xsl:if>   
     <xsl:if test="temperature &lt;= 20"> 
      <xsl:copy> 
       <xsl:apply-templates select="//temperature|node()"/> 
      </xsl:copy> 
     </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

溫度<預期輸出文件超過20

<?xml version="1.0" ?> 
<message-in> 
    <realised-gps> 
     <id>64172068</id> 
     <resourceId>B-06- KXO</resourceId> 
      <position> 
       <coordinatesystem>Standard</coordinatesystem> 
       <latitude>44.380765</latitude> 
       <longitude>25.9952</longitude> 
      </position> 
     <time>2011-05- 23T10:34:46</time> 
     <temperature>15</temperature> 
     <door>0</door> 
    </realised-gps> 
</message-in> 

回答

3

而不是做這個的....

<xsl:if test="temperature &gt; 20"> 

你需要做到這一點...

<xsl:if test=". &gt; 20"> 

因爲你在一個模板已經匹配temperature,測試temperature &gt; 20打算尋找一個也稱爲temperature的子元素,當你真正想要檢查的是當前節點的值。

此外,而不是這樣做,這將最終遞歸匹配相同的模板

<xsl:apply-templates select="//temperature|node()"/> 

你可以做這個....

<xsl:apply-templates /> 

所以你的模板可能看起來這...

<xsl:template match="temperature"> 
     <xsl:if test=". &gt; 20"> 
      <xsl:apply-templates /> 
     </xsl:if>   
     <xsl:if test=". &lt;= 20"> 
      <xsl:copy> 
       <xsl:apply-templates /> 
      </xsl:copy> 
     </xsl:if> 
</xsl:template> 

但是,有一個簡單的方法。取而代之的是上面的模板,只需更具體與模板匹配您要刪除的節點....

<xsl:template match="temperature[. &gt; 20]" /> 

試試這個XSLT

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

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

    <xsl:template match="temperature[. &gt; 20]" /> 
</xsl:stylesheet> 
+0

感謝的人它完美的作品!也感謝您的教訓! – orosco03