代码之家  ›  专栏  ›  技术社区  ›  orosco03

如果温度>20,XSL删除标记,否则复制标记

  •  2
  • orosco03  · 技术社区  · 8 年前

    所以我必须用条件转换这个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>
    

    这只是删除了标记,我无法实现“否则”或“其他如果”条件

    <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>
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Tim C    8 年前

    而不是这样做。。。。

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

    你需要这样做。。。

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

    因为你在模板匹配中 temperature 已经,测试 temperature &gt; 20 将查找一个子元素,也称为 温度 ,而实际上您只想检查当前节点的值。

    此外,不是这样做,而是递归地匹配同一个模板

    <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>