Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A simple XSLT 2.0 stylesheet that iterates over all items and selects the corresponding prices/quantities according to the current position, could look like this:</p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:template match="invoice"&gt; &lt;xsl:variable name="fields" select="field"/&gt; &lt;invoice&gt; &lt;xsl:for-each select="tokenize(field[@name='item'], ';')"&gt; &lt;xsl:variable name="pos" select="position()"/&gt; &lt;item&gt; &lt;desc&gt; &lt;xsl:value-of select="."/&gt; &lt;/desc&gt; &lt;price&gt; &lt;xsl:value-of select="tokenize($fields[@name='price'], ';')[position()=$pos]"/&gt; &lt;/price&gt; &lt;quantity&gt; &lt;xsl:value-of select="tokenize($fields[@name='quantity'], ';')[position()=$pos]"/&gt; &lt;/quantity&gt; &lt;/item&gt; &lt;/xsl:for-each&gt; &lt;/invoice&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>If you want to use XSLT 1.0 together with the EXSLT extension module <em>strings</em>, the stylesheet must be modified only slighly:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" extension-element-prefixes="str"&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:template match="invoice"&gt; &lt;xsl:variable name="fields" select="field"/&gt; &lt;invoice&gt; &lt;xsl:for-each select="str:tokenize(field[@name='item'], ';')"&gt; &lt;xsl:variable name="pos" select="position()"/&gt; &lt;item&gt; &lt;desc&gt; &lt;xsl:value-of select="."/&gt; &lt;/desc&gt; &lt;price&gt; &lt;xsl:value-of select="str:tokenize($fields[@name='price'], ';')[position()=$pos]"/&gt; &lt;/price&gt; &lt;quantity&gt; &lt;xsl:value-of select="str:tokenize($fields[@name='quantity'], ';')[position()=$pos]"/&gt; &lt;/quantity&gt; &lt;/item&gt; &lt;/xsl:for-each&gt; &lt;/invoice&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre>
 

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