Subject: Re: [xsl] 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 |
~/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: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 $
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Re: Add element at the end of, Chris Papademetrious | Thread | Re: [xsl] Add element at the end of, G. Ken Holman g.ken. |
Re: [xsl] Add element at the end of, Wendell Piez wapiez@ | Date | Re: [xsl] Add element at the end of, Liam R. E. Quin liam |
Month |