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

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

Subject: 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 00:12:12 -0000
Re:  Add element at the end of a variable group of  ele
I would approach this as a grouping issue and not a matching issue.

I hope this helps.

. . . . . Ken

~/t/ftemp $ cat charles.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <foo/>
   <bar/>
   <mercury/>
   <venus/>
   <earth earth-type="round"/>
   <earth earth-type="flat"/>
   <mars/>
</root>
~/t/ftemp $ xslt2 charles.xml charles.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 $ cat charles.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:if test="xsd:boolean(current-grouping-key())">
        <moon>Now with more craters!</moon>
      </xsl:if>
      <xsl:copy-of select="current-group()"/>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
~/t/ftemp $ cat charles2.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <foo/>
   <bar/>
   <mercury/>
   <venus/>
   <mars/>
</root>
~/t/ftemp $ xslt2 charles2.xml charles.xsl
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <foo/>
   <bar/>
   <mercury/>
   <venus/>
   <moon>Now with more craters!</moon>
   <mars/>
</root>
~/t/ftemp $


At 2021-03-03 00:01 +0000, Charles O'Connor coconnor@xxxxxxxxxxxx wrote:
Hi all,

Using XSLT 2.0, I'd like to add an element at the end of a group of elements that may vary whether they exist.

This seems like a classic case that people have to deal with all the time, but I've searched without luck, and I'm on maybe lesson 4 of ??? of the online XSLT course I've been taking for the past decade.

So, given the XML

<root>
   <foo/>
   <bar/>
   <mercury/>
   <venus/>
   <earth earth-type="round"/>
   <earth earth-type="flat"/>
   <mars/>
</root>

I want to add <moon>Now with more craters!</moon> after the <foo>, <bar>, <mercury>, <venus> group of elements and before the <earth>, <mars> group of elements, BUT I only know for sure that <foo> and <mars> will exist in the input XML. All the others are optional.

In the Oxygen XPath test window, "root/(foo|bar|mercury|venus)[last()]" got me where I wanted to be, but alas, parentheses are not allowed in the @match for the template.

Thanks!
Charles

Charles O'Connor [he/him] l Business Analyst
Aries Systems Corporation l www.ariessys.com
50 High Street, Suite 21 l North Andover, MA l 01845 l USA
Direct: 802-585-5655 Main: 978-975-7570 l Help Desk: 978-291-1966 | Fax: 978-975-3811





This message is confidential and is intended solely for the use of the individual(s) to whom it is addressed.
If you have received this message in error, do not open any attachment but please notify the sender (above) and delete this message from your system.




--
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.