Search

So, the pipe operator | doesn't actually do what I thought it did, I thought it was 'or' (that's because of PHP). It turns out it's a union operator to join node sets.

The question I have is, can I use the or operator in it's place in a template match?

I'm wanting to match the first node-set I specify or the next one if the first doesn't match anything, and only if it doesn't match anything...

<xsl:apply-templates select="node-set/entry or another-set/entry"/>

If both are present, I want to only match the first one. Is that possible?

Luckily so far I havent run into any issues using | in place of or because I haven't had both nodes present, but it will happen soon....

I am not sure you can do it in one single Xpath expression but you can do the following:

<xsl:apply-templates select="node-set/entry | another-set/entry"/>

<xsl:template match="entry" /> <!-- don't display anything -->

<xsl:template match="entry[1]"> <!-- override only the first one with the actual display-->
   lovely content
</xsl:template>

I've only used | as separator in my template match attribute. In that context it can be used to match multiple nodes:

<xsl:template match="articles/entry | blogposts/entry" />

This will apply the template to both select statements

<xsl:apply-templates select="/data/articles/entry" />
<xsl:apply-templates select="/data/blogposts/entry" />

In your case, I would add logic in the form of <xsl:if /> or <xsl:choose> to determine if the first nodes-set has any entries and execute the correct apply-templates selection based on that

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details