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

Re: Re: Add element at the end of a variable group of

Subject: Re: Re: Add element at the end of a variable group of elements
From: "G. Ken Holman g.ken.holman@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 3 Mar 2021 02:44:29 -0000
Re:  Re: Add element at the end of a variable group of
Chris, how would you like it if someone dropped an unexpected change in a spec on you at the last minute?! :{)} (you know I'm kidding, right?)

Thankfully, your requirement is readily met by turning my solution around only a slight bit. Instead of looking at it as the new data preceding identified content, consider it as new data following identified content.

I hope this helps.

. . . . . Ken

~/t/ftemp $ cat chris.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <foo/>
</root>
~/t/ftemp $ xslt2 chris.xml chris.xsl
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <foo/>
   <moon>Now with more craters!</moon>
</root>
~/t/ftemp $ xslt2 charles.xml chris.xsl
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <foo/>
   <bar/>
   <mercury/>
   <venus/>
   <moon>Now with more craters!</moon>
   <earth earth-type="round"/>
   <earth earth-type="flat"/>
   <mars/>
</root>
~/t/ftemp $ xslt2 charles2.xml chris.xsl
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <foo/>
   <bar/>
   <mercury/>
   <venus/>
   <moon>Now with more craters!</moon>
   <mars/>
</root>
~/t/ftemp $ cat chris.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xsd"
  version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="root">
  <xsl:copy>
    <xsl:for-each-group select="*"
                        group-adjacent="self::earth or self::mars">
      <xsl:copy-of select="current-group()"/>
      <xsl:if test="not(xsd:boolean(current-grouping-key()))">
        <moon>Now with more craters!</moon>
      </xsl:if>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
~/t/ftemp $


At 2021-03-03 00:11 +0000, Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx wrote:
Hi Charles,

I'm interested to see the solutions to this problem. In particular, I'm hoping there is a solution that can handle *no* elements before the insertion point:

<root>
 <!-- moon should go here -->
 <mars/>
</root>

as well as no elements after the insertion point:

<root>
  <foo/>
  <!-- moon should go here -->
</root>

as input. I have a similar situation where I need to insert a new element into a schema-constrained document, and I have to consider the positions of potentially existing (or not) elements.

- Chris



--
Contact info, blog, articles, etc. http://www.CraneSoftwrights.com/s/ |
Check our site for free XML, XSLT, XSL-FO and UBL developer resources |
Streaming hands-on XSLT/XPath 2 training class @US$125 (5 hours free) |
Essays (UBL, XML, etc.) http://www.linkedin.com/today/author/gkholman |

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.