Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this ...</p> <h1>Solution 1: Procedural</h1> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:template match="*" /&gt; &lt;xsl:template match="/"&gt; &lt;documents&gt; &lt;xsl:apply-templates select="document/sheet/line[@id='0']/field[@id!='0']" /&gt; &lt;/documents&gt; &lt;/xsl:template&gt; &lt;xsl:template match="field[@id!='0']"&gt; &lt;document&gt; &lt;header&gt; &lt;number&gt;&lt;xsl:value-of select="." /&gt;&lt;/number&gt; &lt;/header&gt; &lt;line&gt; &lt;xsl:call-template name="line-item-template" &gt; &lt;xsl:with-param name="value-id" select="@id" /&gt; &lt;xsl:with-param name="lines" select="../../line[@id!='0']"/&gt; &lt;/xsl:call-template&gt; &lt;/line&gt; &lt;/document&gt; &lt;/xsl:template&gt; &lt;xsl:template name="line-item-template"&gt; &lt;xsl:param name="value-id" /&gt; &lt;xsl:param name="lines" /&gt; &lt;xsl:for-each select="$lines[field[@id=$value-id]!='']" &gt; &lt;line-item&gt; &lt;lineNumber&gt;&lt;xsl:value-of select="format-number(position(),'00')" /&gt;&lt;/lineNumber&gt; &lt;Code&gt;&lt;xsl:value-of select="field[@id='0']" /&gt;&lt;/Code&gt; &lt;value&gt;&lt;xsl:value-of select="field[@id=$value-id]" /&gt;&lt;/value&gt; &lt;/line-item&gt; &lt;/xsl:for-each &gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>.. or try this ...</p> <h1>Solution 2: Template-o-phile</h1> <p>This solution hooks into the node that relates to the output node.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:template match="*" /&gt; &lt;xsl:template match="/"&gt; &lt;documents&gt; &lt;xsl:apply-templates select="document/sheet/line[@id='0']/field[@id!='0']" /&gt; &lt;/documents&gt; &lt;/xsl:template&gt; &lt;xsl:template match="field[parent::line[@id='0']][@id!='0']"&gt; &lt;document&gt; &lt;header&gt; &lt;number&gt;&lt;xsl:value-of select="." /&gt;&lt;/number&gt; &lt;/header&gt; &lt;line&gt; &lt;xsl:variable name="value-index" select="@id" /&gt; &lt;xsl:apply-templates select="../../line[@id!='0']/field[@id=$value-index]" /&gt; &lt;/line&gt; &lt;/document&gt; &lt;/xsl:template&gt; &lt;xsl:template match="field[parent::line[@id!='0']][.!='']"&gt; &lt;xsl:variable name="current" select="." /&gt; &lt;xsl:variable name="value-index" select="@id" /&gt; &lt;line-item&gt; &lt;lineNumber&gt;&lt;xsl:value-of select="format-number( count( preceding::line[field[@id=$value-index]!=''] [..=$current/../..] ),'00')" /&gt;&lt;/lineNumber&gt; &lt;Code&gt;&lt;xsl:value-of select="../field[@id='0']" /&gt;&lt;/Code&gt; &lt;value&gt;&lt;xsl:value-of select="." /&gt;&lt;/value&gt; &lt;/line-item&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <hr> <p>Now imagine solving this problem for XSLT 2.0. Now that would be fun! :-)</p>
 

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