DailyFrankPeter
DailyFrankPeter

Reputation: 414

XSLT detect a missing tag

I'm having to do an awkward thing of imitating XSD behaviour with XSLT.

In this particular case I'd like to detect a missing element inside a parent element. So far I've come up with the following:

  <xsl:template match="grandparenttagname/parenttagname">    
    <!--missing data guard for this template-->
    <xsl:if test="not(tag1name and tag2name and tag3name)">
      A necessary tag is missing!
    </xsl:if>
    ...

I was wondering whether it was possible to format the condition in such a way that, inside the conditional xsl tag, I'm able to print the condition that failed (i.e. the first of the tag was missing)?

Thanks for any hints.

Upvotes: 2

Views: 1525

Answers (1)

Michael Kay
Michael Kay

Reputation: 163448

There's no magic here, just use three conditions:

<xsl:if test="not(tag1name)">
  tag1name is missing!
</xsl:if>
<xsl:if test="not(tag2name)">
  tag2name is missing!
</xsl:if>
<xsl:if test="not(tag3name)">
  tag3name is missing!
</xsl:if>

Upvotes: 3

Related Questions