首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 ><xsl:if>条件仍未满足输出

<xsl:if>条件仍未满足输出
EN

Stack Overflow用户
提问于 2019-03-08 13:54:56
回答 1查看 53关注 0票数 0

给定下面的XML和相应的XSL代码,当条件不满足时,我们为什么要得到输出?

据我所知,由于存在多个will节点,因此它将考虑第一个节点,然后它将看到该条件为false,因此不应该显示输出。

XML :

代码语言:javascript
复制
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="02-01.xsl"?>

<ancient_wonders>
    <wonder>
        <name language="English">Colossus of Rhodes1</name>
        <name language="Greek">Κολοσσός της Ρόδου1</name>
        <location>Rhodes, Greece</location>
        <height units="feet">107</height>
        <main_image filename="colossus.jpg" w="528" h="349"/>
        <source sectionid="101" newspaperid="21"></source>
    </wonder>

    <wonder>
        <name language="English">Colossus of Rhodes2</name>
        <name language="Greek">Κολοσσός της Ρόδου2</name>
        <location>Rhodes, Greece</location>
        <height units="feet">120</height>
        <main_image filename="colossus.jpg" w="528" h="349"/>
        <source sectionid="103" newspaperid="21"></source>
    </wonder>


</ancient_wonders>

XSLT :

代码语言:javascript
复制
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <!-- Output Method -->
    <xsl:output method="html"/>

    <!-- Root Template -->
    <xsl:template match="/">

        <html>
            <body>

                <p><b>Output 1 :</b></p>
                <xsl:if test="ancient_wonders/wonder/height != 107">
                    <p>Height = <xsl:value-of select="." /></p>
                </xsl:if>

                <p><b>Output 2 :</b></p>
                <xsl:if test="ancient_wonders/wonder/height != 107">
                    <p>Height = <xsl:value-of select="ancient_wonders/wonder/height" /></p>
                </xsl:if>

            </body>
        </html>

    </xsl:template>

</xsl:stylesheet>

输出:

代码语言:javascript
复制
Output 1 :

Height = Colossus of Rhodes1 Κολοσσός της Ρόδου1 Rhodes, Greece 107 Colossus of Rhodes2 Κολοσσός της Ρόδου2 Rhodes, Greece 120

Output 2 :

Height = 107
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-08 14:05:54

这句话:

代码语言:javascript
复制
ancient_wonders/wonder/height

在文档中选择两个height元素。测试:

代码语言:javascript
复制
test="ancient_wonders/wonder/height != 107"

返回true,因为比较节点集中至少有一个满足条件(请参阅:https://www.w3.org/TR/1999/REC-xpath-19991116/#booleans )的节点。

只有xsl:value-of指令具有只返回所选节点集的第一个节点的值的异常;这种异常在XSLT2.0中被删除。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55064686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档