xsl-list
[Top] [All Lists]

Re: [xsl] Add id to next element

2022-05-19 22:52:05
On Fri, May 20, 2022 at 9:12 AM Kevin Brown
kevin(_dot_)brown(_at_)xportability(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>
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.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>