2017-11-25 116 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