Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is what I would do:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" &gt; &lt;!-- identity template to copy everything unless otherwise noted --&gt; &lt;xsl:template match="node()|@*"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;!-- match the first text node directly following a &lt;c&gt; element --&gt; &lt;xsl:template match="text()[preceding-sibling::node()[1][self::c]]"&gt; &lt;!-- ...and change its contents --&gt; &lt;xsl:text&gt;Hello world&lt;/xsl:text&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Note that text nodes contain "surrounding" whitespace - in the sample XML in the question the matched text node is whitespace only, which is why the above works. It will stop to work as soon as the input document looks like this:</p> <pre><code>&lt;a&gt;&lt;b/&gt;&lt;c/&gt;&lt;/a&gt; </code></pre> <p>because here is no text node following <code>&lt;c&gt;</code>. So if this is too brittle for your use case, an alternative would be:</p> <pre><code>&lt;!-- &lt;c&gt; nodes get a new adjacent text node --&gt; &lt;xsl:template match="c"&gt; &lt;xsl:copy-of select="." /&gt; &lt;xsl:text&gt;Hello world&lt;/xsl:text&gt; &lt;/xsl:template&gt; &lt;!-- make sure to remove the first text node directly following a &lt;c&gt; node--&gt; &lt;xsl:template match="text()[preceding-sibling::node()[1][self::c]]" /&gt; </code></pre> <p>In any case, stuff like the above makes clear why intermixing of text nodes and element nodes is best avoided. This is not always possible (see XHTML). But when you have the chance and the XML is supposed to be purely a container for structural data, staying clear of mixed content makes your life easier.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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