Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>This is one fairly short and easy solution</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ax21="my:ax21" exclude-result-prefixes="ax21" &gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:variable name="vCols" select="/*/ax21:columnNames"/&gt; &lt;xsl:variable name="vNumCols" select="count($vCols)"/&gt; &lt;xsl:template match="ax21:result"&gt; &lt;result&gt; &lt;xsl:apply-templates select= "ax21:resultData[position() mod $vNumCols = 1]" /&gt; &lt;/result&gt; &lt;/xsl:template&gt; &lt;xsl:template match="ax21:resultData"&gt; &lt;item&gt; &lt;xsl:apply-templates mode="create" select= "(.|following-sibling::ax21:resultData) [not(position() &gt; $vNumCols) ] "/&gt; &lt;/item&gt; &lt;/xsl:template&gt; &lt;xsl:template match="ax21:resultData" mode="create"&gt; &lt;xsl:variable name="vPos" select="position()"/&gt; &lt;xsl:element name="{$vCols[$vPos]}"&gt; &lt;xsl:value-of select="."/&gt; &lt;xsl:if test="not(text())"&gt; &lt;xsl:value-of select= "(.| preceding-sibling::ax21:resultData) [position() mod $vNumCols = $vPos] [text()] [last()] "/&gt; &lt;/xsl:if&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied on the following XML document</strong>:</p> <pre><code>&lt;ax21:result type="test.ws.Result" xmlns:ax21="my:ax21" &gt; &lt;ax21:columnNames&gt;fileName&lt;/ax21:columnNames&gt; &lt;ax21:columnNames&gt;lockedState&lt;/ax21:columnNames&gt; &lt;ax21:columnNames&gt;currentLockOwner&lt;/ax21:columnNames&gt; &lt;ax21:columnNames&gt;UUID&lt;/ax21:columnNames&gt; &lt;ax21:resultData&gt;Test1.doc&lt;/ax21:resultData&gt; &lt;ax21:resultData&gt;true&lt;/ax21:resultData&gt; &lt;ax21:resultData&gt;analyst&lt;/ax21:resultData&gt; &lt;ax21:resultData&gt;f48f0450-9ecc-4a44-b063-898d9d72d112&lt;/ax21:resultData&gt; &lt;ax21:resultData&gt;Test2.doc&lt;/ax21:resultData&gt; &lt;ax21:resultData&gt;false&lt;/ax21:resultData&gt; &lt;ax21:resultData/&gt; &lt;ax21:resultData&gt;f48f0450-9ecc-4a44-b063-898d9d72d112&lt;/ax21:resultData&gt; &lt;ax21:resultData&gt;Test3.doc&lt;/ax21:resultData&gt; &lt;ax21:resultData&gt;true&lt;/ax21:resultData&gt; &lt;ax21:resultData&gt;analyst&lt;/ax21:resultData&gt; &lt;ax21:resultData&gt;f48f0450-9ecc-4a44-b063-898d9d72d112&lt;/ax21:resultData&gt; &lt;ax21:resultData&gt;Test4.doc&lt;/ax21:resultData&gt; &lt;ax21:resultData&gt;false&lt;/ax21:resultData&gt; &lt;ax21:resultData/&gt; &lt;ax21:resultData&gt;f48f0450-9ecc-4a44-b063-898d9d72d112&lt;/ax21:resultData&gt; &lt;/ax21:result&gt; </code></pre> <p><strong>the wanted result is produced</strong>:</p> <pre><code>&lt;result&gt; &lt;item&gt; &lt;fileName&gt;Test1.doc&lt;/fileName&gt; &lt;lockedState&gt;true&lt;/lockedState&gt; &lt;currentLockOwner&gt;analyst&lt;/currentLockOwner&gt; &lt;UUID&gt;f48f0450-9ecc-4a44-b063-898d9d72d112&lt;/UUID&gt; &lt;/item&gt; &lt;item&gt; &lt;fileName&gt;Test2.doc&lt;/fileName&gt; &lt;lockedState&gt;false&lt;/lockedState&gt; &lt;currentLockOwner&gt;analyst&lt;/currentLockOwner&gt; &lt;UUID&gt;f48f0450-9ecc-4a44-b063-898d9d72d112&lt;/UUID&gt; &lt;/item&gt; &lt;item&gt; &lt;fileName&gt;Test3.doc&lt;/fileName&gt; &lt;lockedState&gt;true&lt;/lockedState&gt; &lt;currentLockOwner&gt;analyst&lt;/currentLockOwner&gt; &lt;UUID&gt;f48f0450-9ecc-4a44-b063-898d9d72d112&lt;/UUID&gt; &lt;/item&gt; &lt;item&gt; &lt;fileName&gt;Test4.doc&lt;/fileName&gt; &lt;lockedState&gt;false&lt;/lockedState&gt; &lt;currentLockOwner&gt;analyst&lt;/currentLockOwner&gt; &lt;UUID&gt;f48f0450-9ecc-4a44-b063-898d9d72d112&lt;/UUID&gt; &lt;/item&gt; &lt;/result&gt; </code></pre> <p><strong>Explanation</strong>:</p> <ol> <li><p>For convenience the column names and their number are collected in the global variables <code>$vCols</code> and <code>$vNumCols</code>.</p></li> <li><p>We are applying templates to every N-th <code>ax21:resultData</code> element, where <code>N mod $vNumCols = 1</code> . Every such element starts a new <code>item</code>.</p></li> <li><p>Every <code>ax21:resultData</code> element that will be the first in an <code>item</code> is matched by a template in "no-mode". THis simply creates the wrapping <code>item</code> element and applies to all current $vNumCols <code>ax21:resultData</code> elements another template -- in <code>"create"</code> mode.</p></li> <li><p>The template in <code>"create"</code> mode simply creates an element, whose name is the value of the n-th element in <code>$vCols</code>, where <code>n</code> is the <code>position()</code> of the current() node to which the template is being applied.</p></li> <li><p>Finally, if it happens that no value was supplied, we get (in backwards order) the latest non-emptyvalue for the same type of element.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    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.
 

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