Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For the selection of inner <code>g</code> elements, you can rely on the <code>position()</code> function. For the <code>text</code> elements, simply check whether there are any following siblings.</p> <p>Also, note that it is essential to qualify the elements with the SVG namespace.</p> <p><strong>Stylesheet</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:svg="http://www.w3.org/2000/svg"&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="svg:svg|svg:g[parent::svg:svg]"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;!-- Match all g elements that are desendents of a g element --&gt; &lt;xsl:template match="svg:g[parent::svg:g]"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="position() = 1"&gt; &lt;xsl:copy&gt; &lt;xsl:copy-of select="*|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise/&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;xsl:template match="svg:text"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="not(following-sibling::*)"&gt; &lt;xsl:copy&gt; &lt;xsl:value-of select="."/&gt; &lt;/xsl:copy&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise/&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>Output</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"&gt; &lt;g&gt; &lt;g&gt; &lt;defs&gt; &lt;rect/&gt; &lt;/defs&gt; &lt;clipPath/&gt; &lt;polygon/&gt; &lt;/g&gt; &lt;text&gt;Keep this text&lt;/text&gt; &lt;/g&gt; &lt;/svg&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload