如何检查XPathed元素是否存在

时间:2013-09-15 21:59:30

标签: xslt xpath

我有一个XPath表达式,我想在XSLT中使用

checks/check/INFOS/INFO[msg[starts-with(@id,"Start")] and not(msg[starts-with(@id,"Finished")])]

它检查以下XML:

<checks>
  <check id="a" level="INFO">
        <INFOS>
            <INFO id="">
                <msg id="Start checking"/>
                <msg id="xxx"/>
                <msg id="Finished checking"/>
            </INFO>
        </INFOS>
    </check>
    <check id="b" level="INFO">
        <INFOS>
            <INFO id="">
                <msg id="Start checking ."/>
                <msg id="yyy"/>
            </INFO>
        </INFOS>
    </check>
</checks>

找到/返回节点:

<INFO id=""> 
  <msg id="Start checking ."/>  
  <msg id="yyy"/> 
</INFO>

所以没关系。但问题是,如果返回这个节点怎么能转换?或者如何检查它是否已退回?它存在?

1 个答案:

答案 0 :(得分:2)

如果您在XSLT中的<xsl:apply-templates />元素上调用<checks>,您应该能够在信息模板上找到适当的匹配信息,例如:

<xsl:template match="checks">
    <h1>Here are my Info messages</h1>
    <div id="info">
        <xsl:apply-templates select="check/INFOS/INFO"/>
    </div>
</xsl:template>

<xsl:template match="check/INFOS/INFO[msg[starts-with(@id,"Start")]
                     and not(msg[starts-with(@id,"Finished")])]">
    <!-- Do Something with checks that are start messages -->
</xsl:template>

<xsl:template match="check/INFOS/INFO">
    <!-- Do Something with checks that aren't Start messages -->
    <!-- If you leave this blank, nothing will be done for them. -->
</xsl:template>