Note that there are some explanatory texts on larger screens.

plurals
  1. POXML Key/Value manipulation using XSLT
    text
    copied!<p>I have data like this - </p> <pre><code>&lt;item&gt; &lt;name&gt;Bob&lt;/name&gt; &lt;fav_food&gt;pizza&lt;/fav_food&gt; &lt;key&gt;{Salary}&lt;/key&gt; &lt;value&gt;1000&lt;/value&gt; &lt;/item&gt; </code></pre> <p>I want my output to look like this - </p> <pre><code>&lt;item&gt; &lt;name&gt;Bob&lt;/name&gt; &lt;fav_food&gt;pizza&lt;/fav_food&gt; &lt;Salary&gt;1000&lt;/Salary&gt; &lt;/item&gt; </code></pre> <p>Edit, instead of just a value, if I had other tags only one of which is guaranteed to be nonempty like so, what is wrong with my transform? I'm using Sean's XSLT 1.0 transform as a source.</p> <p>Input - </p> <pre><code>&lt;item&gt; &lt;name&gt;Bob&lt;/name&gt; &lt;fav_food&gt;pizza&lt;/fav_food&gt; &lt;key&gt;{Salary}&lt;/key&gt; &lt;value /&gt; &lt;value2&gt;1000&lt;/value2&gt; &lt;value3 /&gt; &lt;/item&gt; </code></pre> <p>Desired output - </p> <pre><code>&lt;item&gt; &lt;name&gt;Bob&lt;/name&gt; &lt;fav_food&gt;pizza&lt;/fav_food&gt; &lt;Salary&gt;1000&lt;/Salary&gt; &lt;/item&gt; </code></pre> <p>My current transform - </p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output indent="yes" omit-xml-declaration="yes" /&gt; &lt;xsl:strip-space elements="*" /&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;xsl:template match="key"&gt; &lt;xsl:element name="{substring-before(substring-after(.,'{'),'}')}"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="value != ''"&gt; &lt;xsl:value-of select="following-sibling::value" /&gt; &lt;/xsl:when&gt; &lt;xsl:when test="value2 != ''"&gt; &lt;xsl:value-of select="following-sibling::value2" /&gt; &lt;/xsl:when&gt; &lt;xsl:when test="value3 != ''"&gt; &lt;xsl:value-of select="following-sibling::value3" /&gt; &lt;/xsl:when&gt; &lt;/xsl:choose&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&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