Re: [xsl] Add id to next element

Subject: Re: [xsl] Add id to next element
From: "Eliot Kimber eliot.kimber@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 19 May 2022 22:03:12 -0000
Nothing wrong with that formulationbI guess I have enough XSLT 2 brain
damage that I forget about being able to select the head of a possibly
multi-item sequence as a shortcut for the if/then/else I wrote p


I spend a lot of time (or have spent) correcting older XSLT code to use
exactly that formulation so I should really start there b&

Cheers,

E.

_____________________________________________
Eliot Kimber
Sr Staff Content Engineer
O: 512 554 9368
M: 512 554 9368
servicenow.com<https://www.servicenow.com>
LinkedIn<https://www.linkedin.com/company/servicenow> |
Twitter<https://twitter.com/servicenow> |
YouTube<https://www.youtube.com/user/servicenowinc> |
Facebook<https://www.facebook.com/servicenow>

From: Graydon graydon@xxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thursday, May 19, 2022 at 1:47 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: Re: [xsl] Add id to next element
[External Email]


On Thu, May 19, 2022 at 05:53:58PM -0000, Eliot Kimber
eliot.kimber@xxxxxxxxxxxxxx scripsit:
> The typical way to do it is to have the processing of the section element
get
> the value from the preceding target element:
>
>   <xsl:template match="section">
>     <xsl:variable name="id" as="xs:string?"
>       select="preceding-sibling::target[1]/@refid"
>     />
>     <xsl:copy>
>         <xsl:attribute name="id"
>           select="if ($id) then $id else generate-id(.)"
>         />
>         <xsl:apply-templates mode="#current"/>
>     </xsl:copy>
>  </xsl:template>

What's wrong with

<xsl:template match="section">
    <xsl:copy>
        <xsl:apply-templates select="@*" mode="#current" />
        <xsl:attribute name="id"
          select="(preceding-sibling::*[1][self::target]/@refid,generate-id(.
)) => head()" />
        <xsl:apply-templates mode="#current" />
    </xsl:copy>
</xsl:template>

I am assuming XSLT 3, here, but that should be a fairly safe assumption
these days.

--
Graydon Saunders  | graydonish@xxxxxxxxx
CC&s oferC)ode, C0isses swC! mC&g.
-- Deor  ("That passed, so may this.")

Current Thread