Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply-templates to all templates except a spefic one

Tags:

xslt

xslt-2.0

In the code bellow I'm applying all the templates to the element chapter (in the end of the code) but I would like to know how it is possible to apply all the templates to this element except a spefic one. In this case it's the element title because I'm already selecting it in the line before and it appears repeated in the html file. Someone?

<xsl:template match="chapter">
    <h3>
       <a name="{@id}"><xsl:value-of select="title"/></a>
    </h3>
    <xsl:apply-templates/>
</xsl:template>

Output:

<h3>Title</h3>
Title<br>
Text.
like image 788
Zombie Avatar asked Jan 08 '15 19:01

Zombie


People also ask

What is xsl apply templates?

The <xsl:apply-templates> element selects a set of nodes in the input tree and instructs the processor to apply the proper templates to them.

What is the difference between call template and apply template in XSLT?

With <xsl:apply-templates> the current node moves on with every iteration, whereas <xsl:call-template> does not change the current node.

How do I select all in XSLT?

If you need to select all but a specific attribute, use local-name( ) . The self axis, when applied to a name, refers only to elements. In other words, use <xsl:copy-of select=@*[local-name( ) != 'ignored-attribute']/> and not <xsl:copy-of select=@*[not(self::ignored-attribute)]/> .

What is match in XSLT?

The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).


1 Answers

A plain <xsl:apply-templates/> is equivalent to <xsl:apply-templates select="node()" />, i.e. all child nodes. You can exclude certain nodes by using the XPath 2.0 except operator, e.g.

<xsl:apply-templates select="node() except title" />

This would select all child nodes except those that are elements with the name title. If you are only interested in child elements (not text nodes etc.) then you could use * except title instead.

The except operator essentially implements set difference - you're not limited to simple element names on the right, you can use any expression that returns a sequence of nodes, e.g.

node() except (title | div[@class = 'heading'])

X except Y selects all nodes that are in the sequence selected by X and not also in the sequence selected by Y

like image 115
Ian Roberts Avatar answered Oct 06 '22 05:10

Ian Roberts