Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following code do what you try to achieve. However, depending on how the data set can change maybe is not he most efficient approach.</p> <p>I am assuming that YColumns can appear in a different order and that we can find any number of datasets (not just 2) in the source file. If some of those restrictions can be removed, the solution could be simpler.</p> <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt; &lt;xsl:output method="xml" indent="yes" /&gt; &lt;!-- Holds the category to be selected --&gt; &lt;xsl:param name="category" select="'Country'" /&gt; &lt;!-- Process root element --&gt; &lt;xsl:template match="Data"&gt; &lt;data&gt; &lt;xsl:apply-templates select="*" /&gt; &lt;/data&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Rows"&gt; &lt;!-- Generate category based on parameter $category --&gt; &lt;categories&gt; &lt;xsl:apply-templates select="Row/XColumns[Name = $category]" /&gt; &lt;/categories&gt; &lt;!-- Generate data sets --&gt; &lt;xsl:apply-templates select="Row[1]/YColumns" /&gt; &lt;/xsl:template&gt; &lt;!-- Generate category element --&gt; &lt;xsl:template match="XColumns"&gt; &lt;category label="{Value}" /&gt; &lt;/xsl:template&gt; &lt;!-- Generate dataset elements --&gt; &lt;xsl:template match="YColumns"&gt; &lt;xsl:variable name="name" select="Name" /&gt; &lt;dataset seriesName="{$name}"&gt; &lt;xsl:for-each select="../../Row/YColumns[Name = $name]/Value"&gt; &lt;set value="{.}" /&gt; &lt;/xsl:for-each&gt; &lt;/dataset&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>UPDATE: a more efficient approach is using &lt;xsl:key> to index all YColumn names. So you have to add</p> <pre><code>&lt;xsl:key name="data-set" match="YColumns/Value" use="../Name" /&gt; </code></pre> <p>as a direct child of &lt;xsl:stylesheet></p> <p>And change the expression following expression</p> <pre><code>../../Row/YColumns[Name = $name]/Value </code></pre> <p>to</p> <pre><code>key('data-set', $name) </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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