Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>Try this: &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 indent="yes"/&gt; &lt;xsl:template match="@*|node()"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@*|node()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="text()" /&gt; &lt;xsl:template match="PackNumber"&gt; &lt;xsl:if test="not(preceding::PackNumber =.)" &gt; &lt;!-- if previous &lt;PackNumber&gt; is not equal to current &lt;PackNumber&gt; t--&gt; &lt;counter&gt; &lt;xsl:value-of select="count(preceding::PackNumber[not(preceding::PackNumber= .)])+1"/&gt; &lt;/counter&gt; &lt;/xsl:if&gt; &lt;PNumber&gt; &lt;xsl:value-of select="."/&gt; &lt;/PNumber&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/"&gt; &lt;A&gt; &lt;target&gt; &lt;xsl:apply-templates select="//Item"/&gt; &lt;/target&gt; &lt;/A&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>Update</strong> according the changed question:<br> <em>get the count here</em> (inside a for each loop as variable)<br> Try this:</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 indent="yes"/&gt; &lt;xsl:template match="@*|node()"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@*|node()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="text()" /&gt; &lt;xsl:template match="PackNumber" mode="count"&gt; &lt;xsl:value-of select="count(preceding::PackNumber [not(preceding::PackNumber= .)and not( . = current()/. ) ])+1"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/"&gt; &lt;A&gt; &lt;target&gt; &lt;xsl:for-each select="//Item/PackNumber"&gt; &lt;xsl:variable name="count"&gt; &lt;xsl:apply-templates select="." mode="count"/&gt; &lt;/xsl:variable&gt; &lt;counter&gt; &lt;xsl:value-of select="$count"/&gt; &lt;/counter&gt; &lt;PNumber&gt; &lt;xsl:value-of select="."/&gt; &lt;/PNumber&gt; &lt;/xsl:for-each&gt; &lt;/target&gt; &lt;/A&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Update Output:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;A&gt; &lt;target&gt; &lt;counter&gt;1&lt;/counter&gt; &lt;PNumber&gt;1234&lt;/PNumber&gt; &lt;counter&gt;2&lt;/counter&gt; &lt;PNumber&gt;567&lt;/PNumber&gt; &lt;counter&gt;2&lt;/counter&gt; &lt;PNumber&gt;567&lt;/PNumber&gt; &lt;counter&gt;3&lt;/counter&gt; &lt;PNumber&gt;126&lt;/PNumber&gt; &lt;counter&gt;4&lt;/counter&gt; &lt;PNumber&gt;876&lt;/PNumber&gt; &lt;/target&gt; &lt;/A&gt; </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.
 

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