[xsl] clever ways to dynamically copy/create element structure as needed?

Subject: [xsl] clever ways to dynamically copy/create element structure as needed?
From: "Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 1 Oct 2022 16:28:18 -0000
Hi everyone,

I want to use XSLT to add a piece of <data> metadata to a DITA topic. The DITA
content model lets me place it at "topic/prolog/metadata/data":

<topic id="topic">
  <title>My Topic</title>
  <prolog>
    <metadata>
      <data name="prefix">my-prefix</data>
    </metadata>
  </prolog>
  <body/>
</topic>

Some, none, or all of the required "prolog/metadata" element structure might
already exist:

<topic id="topic">
  <title>My Topic</title>
  <body/>
</topic>

<topic id="topic">
  <title>My Topic</title>
  <prolog>
    <author type="creator">chrispy</author>
    <metadata>
      <keywords>
        <indexterm>some term</indexterm>
      </keywords>
    </metadata>
  </prolog>
  <body/>
</topic>

My current XSLT code to do this is embarrassing.

Does anyone have a favorite way of adding an element at a particular hierarchy
path, copying existing levels and creating missing levels as needed?

(This is just an example. I have other scenarios that require deeper element
structures.)

Thanks!

-----
Chris Papademetrious
Tech Writer, Implementation Group
(610) 628-9718 home office
(570) 460-6078 cell

Current Thread