Re: [xsl] Add id to next element

Subject: Re: [xsl] Add id to next element
From: "Raghavendra Nyshadham nyraghu27132@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 20 May 2022 03:52:01 -0000
On Fri, May 20, 2022 at 9:12 AM Kevin Brown
kevin.brown@xxxxxxxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
wrote:

> Unless you need other things in the match of section (and not a straight copy).
> And assuming you are not processing data that would have:
>
> <target refid="foo"/>
> <section>....</section>
> <target refid="bar"/>
> <section>....</section>
>
> Then I am not sure here:
>
> <xsl:attribute name="id" select="preceding-sibling::target/@refid"/>

Right now I am using this:

<xsl:template match="section" as="element(section)">
    <section>
      <xsl:variable name="previous" select="preceding-sibling::*[1]"
as="element()"/>
      <xsl:if test="$previous/local-name() = 'target'">
        <xsl:attribute name="id">
          <xsl:value-of select="$previous/@refid"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </section>
  </xsl:template>

  <xsl:template match="target"/>

It is an adaptation of some of the solutions from the other replies,
and works for the actual XML document I am processing. The id for any
<section> is provided by a <target> that immediately precedes it.  Not
all <section>s have an immediately preceding <target>.

Regards,
Raghavendra.

Current Thread