Search

Hello,

how I could grab elements before and after some element I specify? I mean if I have textarea and it saves XHTML, how I can grab all elements (like p-tag, for example) before hr-tag?

An example:

<p>... some content ...</p>
<p>... and more ...</p>
<hr/>
<p>Another content ...</p>
<p>... and more.</p>

Maybe call / apply template for elements matching all elements before <hr/> tag and another template for all elements after the <hr/> tag.

A similar question has been asked a few days ago, see my response there. :-)

Ok, thanks.

But given the example above, *[perceding-sibling::hr] should grab the elements before the hr-tag, right? The result is opposite...

*[preceding-sibling::hr] should match all elements that come after the hr. Since the template is empty, it will simply display nothing for those.

All other elements (the one before the hr) remain untouched and will be displayed.

To demonstrate it in action I've created a testcase on xpathr.

Odd, since the word preceding indicates otherwise :) How I could then do opposite? With following-sibling?

Edit: Or is it just me not understanding something?

Depends on the context. In the case of

<xsl:template match="*[preceding-sibling::hr]" mode="html" priority="1" />

we are matching elements with the following conditions:

  • * means all elements with any name
  • [preceding-sibling::hr] means among those the ones that have a preceding sibling named hr.

Obviously those conditions can only be met by elements following a hr.

What you're probably thinking of is a select like

<xsl:for-each select="preceding-sibling::hr">...

And you're right, in this situation you would get all hr elements preceding the current context node (also in reverse order). But yeah, that's a totally different use case. :-)

Yep, following-sibling is the complementary axis to preceding-sibling.

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