Note that there are some explanatory texts on larger screens.

plurals
  1. POXSLT adding new nodes if they do not exist
    primarykey
    data
    text
    <p>I am having some trouble using XSLT to check if nodes exist, and if not add them to the document. Here is my situation:</p> <p><strong>Input</strong></p> <pre><code>&lt;Message&gt; &lt;a&gt;123&lt;/a&gt; &lt;c&gt;456&lt;/c&gt; &lt;d&gt;789&lt;/d&gt; &lt;/Message&gt; </code></pre> <p><strong>Desired output</strong></p> <pre><code>&lt;MsgHead&gt; &lt;Document&gt; &lt;Message&gt; &lt;a&gt;123&lt;/a&gt; &lt;b&gt;-1&lt;/b&gt; &lt;c&gt;456&lt;/c&gt; &lt;d&gt;789&lt;/d&gt; &lt;/Message&gt; &lt;Document&gt; &lt;/MsgHead&gt; </code></pre> <p>I am also given the following static file with "default values"</p> <p><strong>Defaults</strong></p> <pre><code>&lt;DefaultNodes&gt; &lt;a&gt;-1&lt;/a&gt; &lt;b&gt;-1&lt;/b&gt; &lt;c&gt;-1&lt;/c&gt; &lt;d&gt;-1&lt;/d&gt; &lt;/DefaultNodes&gt; </code></pre> <p>The input files have varying numbers of nodes and I need to "complete" them with the default nodes that are missing. The node names are obviously not a, b, c, etc, but some 700 different nodes with different default values.</p> <p>Here is my attempt so far <strong>My XSLT</strong></p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:template match="/"&gt; &lt;MsgHead&gt; &lt;Document&gt; &lt;Message&gt; &lt;xsl:apply-templates&gt;&lt;/xsl:apply-templates&gt; &lt;/Message&gt; &lt;/Document&gt; &lt;/MsgHead&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Message"&gt; &lt;xsl:copy-of select="node()"/&gt; &lt;xsl:for-each select="document('default-nodes.xml')/DefaultNodes/*"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="//*[local-name(current())]"&gt; &lt;!-- This is the line giving me trouble --&gt; &lt;!--Node already present, do nothing--&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;!--Node not in input, add from the defaults file --&gt; &lt;xsl:copy-of select="self::node()"/&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>This is almost working, but it seems to not be able to find if the nodes exist or not. The current test I am using (//*[local-name(current())]) seems to return true no matter what. Anyone have any suggestion on how I could fix this?</p> <p>Thanks!</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. This table or related slice is empty.
    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