dantuch
dantuch

Reputation: 9293

How to check if XPathed element exists

I have an XPath expression, that I want to use in XSLT

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

It checks following 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>

found / returned node:

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

So it is ok. But question is, how can I transform such node, if it was returned? or how can I check if it was returned / it exists?

Upvotes: 0

Views: 97

Answers (1)

user764357
user764357

Reputation:

If you call <xsl:apply-templates /> on the <checks> element in your XSLT, you should be able to just have an appropraite match on the template for info messages, like so:

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

Upvotes: 2

Related Questions