Values can be obtained from an attribute in XSLT using the xsl:value-of
or xsl:attribute
element. Once the value is obtained, it can be stored in a variable using the xsl:variable
element.
To change the values of other attributes, the xsl:attribute
element is used, along with an expression that evaluates the new attribute value. For example:
<template>
<xsl:template match="div[@class='old-class']">
<xsl:variable name="newClass" select="'new-class'" />
<div>
<xsl:attribute name="class">
<xsl:value-of select="$newClass" />
</xsl:attribute>
<xsl:apply-templates />
</div>
</xsl:template>
</template>
In this example, the XSLT template matches a div
element with a class of old-class
. The value of new-class
is stored in a variable named newClass
. The xsl:attribute
element is then used to set the value of the class
attribute to the value of the newClass
variable. Finally, the xsl:apply-templates
element is used to process any child nodes of the div
element.
Asked: 2023-06-21 01:05:13 +0000
Seen: 11 times
Last updated: Jun 21 '23