Re: [xsl] targeting the 'first' instance of the same element in variable structures

Subject: Re: [xsl] targeting the 'first' instance of the same element in variable structures
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 2 Jul 2024 10:41:18 -0000
On 02/07/2024 12:34, Jean-Paul Rehr rehrjb@xxxxxxxxx wrote:
> Dear XSL list,
>
> I am trying to target a specific <head> which can appear nested within
> a variable number of <div> and possible <list> elements. In the three
> examples below I have put in red the <head> element I want to target
> with the same XSL template. In my way of thinking, it is always the
> first appearance of <head> no matter how deep or shallow it may be
> nested. But I am having trouble conceiving a path that can take into
> account the variable depth. Any thoughts aboutB how to target this?


You can do

 B  <xsl:template match="(div//head)[1]">

in XSLT 3 (if the processor is up to it, I think early Saxon releases
like 9.8 or 9.9 that are no longer supported, didn't support it, but
current releases (11 and 12) certainly do.


fiddle link
<https://martin-honnen.github.io/cheerpj3-saxonhe12-fiddle/?code=%3C%3Fxml+ve
rsion%3D%221.0%22+encoding%3D%22utf-8%22%3F%3E%0A%3Cxsl%3Astylesheet+xmlns%3A
xsl%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2FXSL%2FTransform%22%0A++version%3D%2
23.0%22%0A++xmlns%3Axs%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%22%0A+
+exclude-result-prefixes%3D%22%23all%22%0A++expand-text%3D%22yes%22%3E%0A++%0
A++%3Cxsl%3Atemplate+match%3D%22%28div%2F%2Fhead%29%5B1%5D%22%3E%0A++++%3Cxsl
%3Acomment%3EMatched+%7Bnode-name%28%29%7D%3C%2Fxsl%3Acomment%3E%0A++++%3Cxsl
%3Anext-match%2F%3E%0A++%3C%2Fxsl%3Atemplate%3E%0A%0A++%3Cxsl%3Aoutput+method
%3D%22html%22+indent%3D%22no%22+html-version%3D%225%22%2F%3E%0A%0A++%3Cxsl%3A
mode+on-no-match%3D%22shallow-copy%22%2F%3E%0A%0A++%3Cxsl%3Atemplate+match%3D
%22%2F%22+name%3D%22xsl%3Ainitial-template%22%3E%0A++++%3Cxsl%3Acopy%3E%0A+++
+++%3Cxsl%3Aapply-templates%2F%3E%0A++++++%3Cxsl%3Acomment%3ERun+with+%7Bsyst
em-property%28%27xsl%3Aproduct-name%27%29%7D+%7Bsystem-property%28%27xsl%3Apr
oduct-version%27%29%7D+at+%7Bcurrent-dateTime%28%29%7D%3C%2Fxsl%3Acomment%3E%
0A++++%3C%2Fxsl%3Acopy%3E%0A++%3C%2Fxsl%3Atemplate%3E%0A%0A%3C%2Fxsl%3Astyles
heet%3E%0A&input=%3Croot%3E%0A++++%3Cdiv%3E%0A++++++++%3Cdiv%3E%0A+++++++++++
+%3Cdiv%3E%0A++++++++++++++++++%3Chead%3ETitle%3C%2Fhead%3E+%3C%21--+nested+i
n+3+divs+--%3E%0A++++++++++++++++++%3Cp%3Esome+content%3C%2Fp%3E%0A++++++++++
+++%3C%2Fdiv%3E%0A++++++++%3C%2Fdiv%3E%0A++++++++%3Cdiv%3E%0A+++++++++++++%3C
head%3ETitle%3C%2Fhead%3E%0A++++++++++++++%3Cp%3Esome+content%3C%2Fp%3E%0A+++
++++++%3C%2Fdiv%3E%0A++++%3C%2Fdiv%3E%0A%0A++++%3Cdiv%3E%0A+++++++++++%3Cdiv%
3E%0A++++++++++++++++++%3Chead%3ETitle%3C%2Fhead%3E+%3C%21--+nested+in+2+divs
+--%3E%0A++++++++++++++++++%3Cp%3Esome+content%3C%2Fp%3E%0A++++++++++++%3C%2F
div%3E%0A++++++++++++%3Cdiv%3E%0A++++++++++++++++++%3Chead%3ETitle%3C%2Fhead%
3E%0A++++++++++++++++++%3Cp%3Esome+content%3C%2Fp%3E%0A+++++++++++%3C%2Fdiv%3
E%0A+++%3C%2Fdiv%3E%0A%0A++++%3Cdiv%3E%0A++++++++%3Cdiv%3E%0A++++++++++++%3Cd
iv%3E%0A+++++++++++++++++%3Clist%3E%0A++++++++++++++++++++++%3Chead%3ETitle%3
C%2Fhead%3E+%3C%21--+nested+in+3+divs+and+list+--%3E%0A++++++++++++++++++++++
%3Citem%3E%3C%2Fitem%3E%0A++++++++++++++++++++++%3Citem%3E%3C%2Fitem%3E%0A+++
++++++++++++++%3C%2Flist%3E%0A+++++++++++++%3C%2Fdiv%3E%0A+++++++++++++%3Cdiv
%3E%0A+++++++++++++++++%3Chead%3ETitle%3C%2Fhead%3E%0A+++++++++++++++++%3Cp%3
Esome+content%3C%2Fp%3E%0A+++++++++++++%3C%2Fdiv%3E%0A++++++++%3C%2Fdiv%3E%0A
++++%3C%2Fdiv%3E%0A%3C%2Froot%3E%0A&code-type=XSLT&input-type=XML>

Current Thread