Reassing attribute value
Posted: Tue Jan 11, 2022 4:47 pm
Hello everyone,
The reason for this topic is that I need a push to write the algorithm that does what I need. I need to do a transformation that changes the value of the @id of each of the elements sequentially. I thought about using an accumulator but it doesn't work and I am running out of ideas. I only need it to reorder the id of the elements sequentially, that is, they are 1,2,3,4 ... so until the last element.
The stylesheet that I wrote:
Thanks for reading
The reason for this topic is that I need a push to write the algorithm that does what I need. I need to do a transformation that changes the value of the @id of each of the elements sequentially. I thought about using an accumulator but it doesn't work and I am running out of ideas. I only need it to reorder the id of the elements sequentially, that is, they are 1,2,3,4 ... so until the last element.
The stylesheet that I wrote:
Code: Select all
<xsl:stylesheet exclude-result-prefixes="xs" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"></xsl:output>
<xsl:mode use-accumulators="#all"/>
<xsl:accumulator name="idsequence" as="xs:integer" initial-value="0" streamable="no">
<xsl:accumulator-rule match="element" select="$value + 1"/>
</xsl:accumulator>
<!-- Copy all elements as they are -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="element/@id">
<xsl:variable name="idvalue" select="accumulator-before('idsequence')"/>
<xsl:attribute name="id" select="$idvalue"></xsl:attribute>
</xsl:template>
</xsl:stylesheet>