Search code examples
xmlxsltdocbook

XSLT: Apply template to Nodes that don't contain a specific subnode


I'm using docbook and am using a template which inserts zero-width characters into my table entries. Which is good, but I need to have the template NOT applied if the table entry includes a <para> element. So, is there a way I can apply the template to all <entry> that do not contain a <para>?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:d="http://docbook.org/ns/docbook">
<xsl:import href="urn:docbkx:stylesheet"/>

...

<xsl:template match="text()[parent::d:entry]">
    <xsl:call-template name="intersperse-with-zero-spaces">
        <xsl:with-param name="str" select="."/>
    </xsl:call-template>
</xsl:template>

...


Solution

  • <xsl:template match="d:entry[not(d:para)]"> matches any entry elements not having any para children. <xsl:template match="d:entry[not(descendant::d:para)]"> matches any entry elements not having any para descendants.

    Or for your posted template you could use <xsl:template match="text()[parent::d:entry[not(d:para)]]">.