Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT detect a missing tag

Tags:

xml

xslt

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.

like image 576
DailyFrankPeter Avatar asked Feb 27 '26 14:02

DailyFrankPeter


1 Answers

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>
like image 163
Michael Kay Avatar answered Mar 02 '26 06:03

Michael Kay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!