Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT matching empty nodes

I have an XML structure like the following:

<Root>
  <!-- ...other stuff -->
  <Events>
    <Event date="0000-00-00">Event Description etc...</Event>
    <Event date="0000-00-00">Event Description etc...</Event>
    <Event date="0000-00-00">Event Description etc...</Event>
  </Events>
  <!-- ...other stuff -->
</Root>

Then I have XSLT in the Stylesheet like so:

<xsl:variable name="Events" select="/Root/Events/Event" />

<xsl:template match="/">
  <!-- Stuff -->    
  <xsl:apply-templates select="$Events" />
  <!-- Stuff -->    
</xsl:template>

<xsl:template match="Event">
  <!-- Regular Event Template Transformation here -->
</xsl:template>    

<!-- ERROR HAPPENS HERE -->
<xsl:template match="not(node())">
  <p class="message">There are currently no upcoming events</p>
</xsl:template>

What I WANT to do is have two templates, one which only shows when there are no events. I KNOW i can use XSLT with <xsl:choose> and <xsl:when> tests to do a count of elements and just call the right template like I would do in procedural languages, but I'm trying to learn how to do this with template processing.

The error I'm getting is : Expected end of the expression, found '('. not -->(<-- node())

like image 252
Armstrongest Avatar asked Dec 10 '22 06:12

Armstrongest


1 Answers

not(node()) is not a valid XSLT pattern, try this:

<xsl:template match="Event">
  <!-- Regular Event Template Transformation here -->
</xsl:template>    

<xsl:template match="Events[not(Event)]">
  <p class="message">There are currently no upcoming events</p>
</xsl:template>
like image 163
Max Toro Avatar answered Jan 11 '23 10:01

Max Toro