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(_dot_)papademetrious(_at_)synopsys(_dot_)com 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 |
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--