[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: clever ways to dynamically copy/create element st

Subject: Re: 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 21:04:39 -0000
Re:  clever ways to dynamically copy/create element  st
I tried implementing your suggested using a moded template. From the default
stylesheet mode, I match the topmost element of the element path I know will
exist, then call an badd-datab moded template on it:


  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="topic">
    <xsl:apply-templates select="." mode="add-data"/>
  </xsl:template>


The moded template successively call itself for each lower level, inserting an
empty element of the next level if it doesnbt already exist:


  <xsl:mode name="add-data" on-no-match="shallow-copy"/>

  <xsl:template match="topic" mode="add-data">
    <xsl:variable name="empty-prolog">
      <prolog/>
    </xsl:variable>
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="title,titlealts,shortdesc,abstract"/>
      <xsl:apply-templates select="(prolog,$empty-prolog)[1]"
mode="add-data"/>
      <xsl:apply-templates select="body,related-links,topic"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="prolog" mode="add-data">
    <xsl:variable name="empty-metadata">
      <metadata/>
    </xsl:variable>
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates
select="author,source,publisher,copyright,critdates,permissions"/>
      <xsl:apply-templates select="(metadata,$empty-metadata)[1]"
mode="add-data"/>
      <xsl:apply-templates
select="change-historylist,resourceid,data,sort-as,data-about,foreign,mathml,
svg-container,unknown"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="metadata" mode="add-data">
    <xsl:apply-templates
select="audience,category,keywords,prodinfo,othermeta"/>
    <xsl:apply-templates select="data[not(@name = 'prefix')]"/>
    <data name="prefix">my-prefix</data>
    <xsl:apply-templates
select="sort-as,data-about,foreign,mathml,svg-container,unknown"/>
  </xsl:template>


This is not too bad, but I wonder if there is still a more elegant way to do
it.

- Chris


From: dvint dvint@xxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Saturday, October 1, 2022 1:50 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Cc: dvint@xxxxxxxxx
Subject: Re:  clever ways to dynamically copy/create element structure as
needed?

I don't have a specific sample and may not be clever. What I have done with
this is to match on the prolog element and then process by the pieces I expect
to be there. So checking for the various child elements of prolog. Test for
their existence and if not there insert what you want. You need to also have a
test that say at the topic/title element checks to see if there is a prolog
element, so you can create one if it isn't there.

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.