Note that there are some explanatory texts on larger screens.

plurals
  1. POxslt1.0 (firefox): reformatting xml code based on the total number of different values present
    primarykey
    data
    text
    <p>This one I really think is not possible to solve through XSLT, so that I will have to do something with JS or just do not implement. But before giving up, of course I have to post here to see if I am wrong and XSLT can do this kind of logic.</p> <p>EDIT: now I'm starting to see that it is possible, getting closer</p> <p>EDIT2: I have to correct a mistake in the XML code provided. The solution should be able to handle multiple nodes with the same values.</p> <p>The concept is that I need to re-format the code including empty tags that reflect the total number of different values. Difficult to explain, much easier to understand looking at the code.</p> <p>Initial XML code</p> <pre><code>&lt;data&gt; &lt;prot seq="AAA"&gt; &lt;node num="4"&gt;1345&lt;/node&gt; &lt;/prot&gt; &lt;prot seq="BBB"&gt; &lt;node num="7"&gt;6666&lt;/node&gt; &lt;/prot&gt; &lt;prot seq="CCC"&gt; &lt;node num="10"&gt;3e33&lt;/node&gt; &lt;/prot&gt; &lt;prot seq="DDD"&gt; &lt;node num="4"&gt;1345&lt;/node&gt; &lt;/prot&gt; &lt;prot seq="EEE"&gt; &lt;node num="10"&gt;3e33&lt;/node&gt; &lt;/prot&gt; &lt;/data&gt; </code></pre> <p>And the wished output</p> <pre><code>&lt;root&gt; &lt;prot seq="AAA"&gt; &lt;node num="4"&gt;1345&lt;/node&gt;&lt;node num="7"&gt;-&lt;/node&gt;&lt;node num="10"&gt;-&lt;/node&gt; &lt;/prot&gt; &lt;prot seq="BBB"&gt; &lt;node num="4"&gt;-&lt;/node&gt;&lt;node num="7"&gt;6666&lt;/node&gt;&lt;node num="10"&gt;-&lt;/node&gt; &lt;/prot&gt; &lt;prot seq="CCC"&gt; &lt;node num="4"&gt;-&lt;/node&gt;&lt;node num="7"&gt;-&lt;/node&gt;&lt;node num="10"&gt;3e33&lt;/node&gt; &lt;/prot&gt; &lt;prot seq="DDD"&gt; &lt;node num="4"&gt;1345&lt;/node&gt;&lt;node num="7"&gt;-&lt;/node&gt;&lt;node num="10"&gt;-&lt;/node&gt; &lt;/prot&gt; &lt;prot seq="EEE"&gt; &lt;node num="4"&gt;-&lt;/node&gt;&lt;node num="7"&gt;-&lt;/node&gt;&lt;node num="10"&gt;3e33&lt;/node&gt; &lt;/prot&gt; &lt;/root&gt; </code></pre> <p>Any idea?</p> <p>Thanks!</p> <p>EDIT3: from Dimitri solution, I've ended up with this simplified solution</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:key name="kNodeByNum" match="/data/prot/node" use="@num"/&gt; &lt;xsl:template match="/"&gt; &lt;root&gt; &lt;xsl:apply-templates select="/data/prot"/&gt; &lt;/root&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/data/prot"&gt; &lt;xsl:variable name="current_num" select="node/@num"/&gt; &lt;xsl:variable name="current_value" select="node"/&gt; &lt;prot seq="{@seq}"&gt; &lt;xsl:for-each select="/data/prot/node[ generate-id() = generate-id(key('kNodeByNum', @num)[1]) ]"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="@num = $current_num"&gt; &lt;node num="{@num}"&gt;&lt;xsl:value-of select="$current_value"/&gt;&lt;/node&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;node num="{@num}"&gt;-&lt;/node&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:for-each&gt; &lt;/prot&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>However, this code also cannot handle the number of nodes in firefox and goes in a forever-like loop (and I have to force firefox to close).</p> <p>But I am thinking that this has nothing to do with the number of nodes but that there is something wrong with the code (?)</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.
 

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