Note that there are some explanatory texts on larger screens.

plurals
  1. POxslt dynamic / conditional apply-template in function of a variable?
    primarykey
    data
    text
    <p>I want to display two different XSLT transformations in function of what the user want. The entire XSL file is the same, except for one line.</p> <p>This line should be as it</p> <pre><code>&lt;xsl:template match="/data/peptides/peptide[generate-id()=generate-id(key('byAccSeq', concat(protein_accessions/accession, '|', sequence))[1])]"&gt; </code></pre> <p>or as it</p> <pre><code>&lt;xsl:template match="/data/peptides/peptide"&gt; </code></pre> <p>My first idea was to create two different .xsl files, and to apply them (javascript) in function of a variable value.</p> <pre><code>var xsltProcessor = new XSLTProcessor(); xsltProcessor.importStylesheet(xslDoc); xhtml = xsltProcessor.transformToFragment(xmlDoc,document); </code></pre> <p>However, it is just a line and I would like to maintain only one file. I would like to do something like this</p> <pre><code>&lt;xsl:param name="variable"/&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$variable = 0"&gt; &lt;xsl:template match="/data/peptides/peptide[generate-id()=generate-id(key('byAccSeq', concat(protein_accessions/accession, '|', sequence))[1])]"&gt; ... &lt;/xsl:template&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:template match="/data/peptides/peptide"&gt; ... &lt;/xsl:template&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; </code></pre> <p>But it does not work.</p> <p>trying with the feature "mode" in the xsl:apply-templates, this code neither works</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="byAccSeq" match="/data/peptides/peptide" use="concat(accession, '|', sequence)"/&gt; &lt;xsl:param name="analysis" select="1"/&gt; &lt;xsl:template match="/"&gt; &lt;root&gt;&lt;name&gt;&lt;xsl:value-of select="$analysis"/&gt;&lt;/name&gt;&lt;xsl:apply-templates select="/data/proteins/protein"/&gt;&lt;/root&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/data/proteins/protein"&gt; &lt;xsl:apply-templates select="/data/peptides/peptide[accession=current()/accession]"/&gt; &lt;/xsl:template&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$analysis=1"&gt; &lt;xsl:apply-templates select="/data/peptides/peptide" mode="one"/&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:apply-templates select="/data/peptides/peptide[accession=current()/accession]" mode="two"/&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;xsl:template match="/data/peptides/peptide" mode="one"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/data/peptides/peptide[generate-id()= generate-id(key('byAccSeq', concat(accession, '|', sequence))[1])]" mode="two"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>-> <a href="http://www.xsltcake.com/slices/sgWUFu/2" rel="nofollow">http://www.xsltcake.com/slices/sgWUFu/2</a></p> <ul> <li>this code is not correct since xsl:choose cannot be a child of xsl:stylesheet</li> </ul> <p>SOLVED (improved below), this is the code that makes what I wanted</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="byAccSeq" match="/data/peptides/peptide" use="concat(accession, '|', sequence)"/&gt; &lt;xsl:param name="analysis" select="1"/&gt; &lt;xsl:template match="/"&gt; &lt;root&gt; &lt;name&gt; &lt;xsl:value-of select="$analysis"/&gt; &lt;/name&gt; &lt;xsl:apply-templates select="/data/proteins/protein"/&gt; &lt;/root&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/data/proteins/protein"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$analysis=1"&gt; &lt;xsl:apply-templates select="/data/peptides/peptide[accession=current()/accession]" mode="one"/&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:apply-templates select="/data/peptides/peptide[accession=current()/accession]" mode="two"/&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/data/peptides/peptide" mode="one"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/data/peptides/peptide" mode="two"/&gt; &lt;xsl:template match="/data/peptides/peptide[generate-id()= generate-id(key('byAccSeq', concat(accession, '|', sequence)))]" mode="two"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>IMPROVED: the final code is much easier to read with much less duplicated code <a href="http://www.xsltcake.com/slices/sgWUFu/6" rel="nofollow">here</a></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:param name="analysis" select="0"/&gt; &lt;xsl:key name="byAcc" match="/data/peptides/peptide" use="accession" /&gt; &lt;xsl:key name="byAccSeq" match="/data/peptides/peptide" use="concat(accession, '|', sequence)"/&gt; &lt;xsl:template match="/"&gt; &lt;root&gt; &lt;name&gt; &lt;xsl:value-of select="$analysis"/&gt; &lt;/name&gt; &lt;xsl:apply-templates select="/data/proteins/protein" /&gt; &lt;/root&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/data/proteins/protein"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$analysis=1"&gt; &lt;xsl:apply-templates select="key('byAcc',accession)" /&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:apply-templates select="key('byAcc',accession)[ generate-id() = generate-id(key('byAccSeq', concat(accession, '|', sequence)))]" /&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/data/peptides/peptide"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>I keep the apply-template calls because I need it, but without them (as in the original code, see comments) is even simpler.</p> <p>Thanks again, not only for answering but for teaching XSLT :)</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