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

Re: inserting a child element while honoring the paren

Subject: Re: inserting a child element while honoring the parent element's content model
From: "Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 25 Feb 2023 02:10:56 -0000
Re:  inserting a child element while honoring the paren
Here is an input document:


<?xml version="1.0" encoding="utf-8" ?>
<topic>
  <title>My Topic</title>
  <prolog>
    <author>chrispy</author>
  </prolog>
  <body><p>Hello!</p></body>
</topic>


and a stylesheet that defines some content models, then inserts some <keyword>
elements in the <topic>:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform
  xmlns:xs=http://www.w3.org/2001/XMLSchema
  xmlns:map=http://www.w3.org/2005/xpath-functions/map
  exclude-result-prefixes="#all"
  version="3.0">

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

  <xsl:output indent="yes"/>

  <!-- define some content models -->
  <xsl:variable name="content-models" as="map(xs:string, xs:string*)"
select="map {
    'topic': ('title', 'titlealts', 'shortdesc', 'abstract', 'prolog', 'body',
'related-links', 'topic'),
    'prolog': ('author', 'source', 'publisher', 'copyright', 'critdates',
'permissions', 'metadata', 'change-historylist', 'resourceid', 'data',
'sort-as', 'data-about', 'foreign', 'mathml', 'svg-container', 'unknown'),
    'metadata': ('audience', 'category', 'keywords', 'prodinfo', 'othermeta',
'data', 'sort-as', 'data-about', 'foreign', 'mathml', 'svg-container',
'unknown'),
    'keywords': ('indexterm', 'keyword')
  }"/>

  <!-- insert some <keyword> elements in a <topic> -->
  <xsl:template match="topic">
    <xsl:apply-templates select="." mode="insert-stuff">
      <xsl:with-param name="path" select="tokenize('prolog/metadata/keywords',
'/')" as="xs:string*"/>
      <xsl:with-param name="content" as="element()*" tunnel="yes">
        <keyword>new keyword 1</keyword>
        <keyword>new keyword 2</keyword>
      </xsl:with-param>
    </xsl:apply-templates>
  </xsl:template>

  <!-- perform content-aware insertion on an element -->
  <xsl:template match="*" mode="insert-stuff">
    ...omitted...
  </xsl:template>

</xsl:stylesheet>


You'll need to insert the mode="insert-stuff" template where indicated; email
size restrictions did not allow everything in one email.

If you call the template with $path set to an empty sequence, then the content
is inserted directly in the current element.

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

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.