Note that there are some explanatory texts on larger screens.

plurals
  1. POxslt 1.0, select group of nodes with key
    primarykey
    data
    text
    <p>I want to select nodes based on some variables. The XML code:</p> <pre><code>&lt;data&gt; &lt;prot seq="AAA"&gt; &lt;node num="1"&gt;1345&lt;/node&gt; &lt;node num="1"&gt;11245&lt;/node&gt; &lt;node num="2"&gt;88885&lt;/node&gt; &lt;/prot&gt; &lt;prot seq="BBB"&gt; &lt;node num="1"&gt;678&lt;/node&gt; &lt;node num="1"&gt;456&lt;/node&gt; &lt;node num="2"&gt;6666&lt;/node&gt; &lt;/prot&gt; &lt;prot seq="CCC"&gt; &lt;node num="1"&gt;111&lt;/node&gt; &lt;node num="1"&gt;222&lt;/node&gt; &lt;node num="2"&gt;333&lt;/node&gt; &lt;/prot&gt; &lt;/data&gt; </code></pre> <p>The XML that I want</p> <pre><code>&lt;output&gt; &lt;prot seq="AAA"&gt; &lt;node num="1"&gt;1345&lt;/node&gt; &lt;node num="2"&gt;88885&lt;/node&gt; &lt;/prot&gt; &lt;prot seq="BBB"&gt; &lt;node num="1"&gt;678&lt;/node&gt; &lt;node num="2"&gt;6666&lt;/node&gt; &lt;/prot&gt; &lt;prot seq="CCC"&gt; &lt;node num="1"&gt;111&lt;/node&gt; &lt;node num="2"&gt;333&lt;/node&gt; &lt;/prot&gt; &lt;/data&gt; </code></pre> <p>So, my idea has been to group the nodes with a xsl:key element, and then do a for-each of them. For example:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/&gt; &lt;xsl:key name="by" match="/data/prot" use="concat(@seq,'|',node/@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:for-each select="./node"&gt; &lt;xsl:for-each select="key('by',concat(current()/../@seq,'|',current()/@num))"&gt; node &lt;xsl:value-of select="./node" /&gt; &lt;/xsl:for-each&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>but the output is not what I expected, and I cannot see what I am doing wrong. I would prefer to keep the for-each structure. It is just as if I was not using properly the xsl:key grouping features.</p> <p>the output that I get, unwanted</p> <pre><code>&lt;root&gt; node 1345 node 1345 node 678 node 678 node 111 node 111&lt;/root&gt; </code></pre> <p>And the code as it to be tested <a href="http://www.xsltcake.com/slices/sgWUFu/20" rel="nofollow">http://www.xsltcake.com/slices/sgWUFu/20</a></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.
 

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