To replace the value of an attribute you first need to locate your element through an Xpath expression, which will be passed to the stylesheet template.
I will assume that the selection is made by the name of xs:element
external that has an attribute name
with the value of NewDataSet
. In this case, the Xpath to locate this element is:
//xs:element[@name='NewDataSet']
This can be passed to the attribute match
of xsl:template
without the //
and will be the context used within the template. You can also use a more specific context by continuing the path to the element where the attribute you want to change is:
xs:element[@name='NewDataSet']/xs:complexType/xs:choice/xs:element
Knowing the element you want to change, you then need to define two templates in the stylesheet. One to copy all other elements and attributes, and one to select the above element and swap its attribute. You also need to declare the namespaces you are using, so that the above-qualified Xpath expressions work.
The XSLT below does the substitution using the criteria described above:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="*|@*">
<xsl:copy>
<xsl:apply-templates select="*|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xs:element[@name='NewDataSet']/xs:complexType/xs:choice/xs:element">
<xsl:copy>
<xsl:attribute name="name">INFO2</xsl:attribute>
<xsl:apply-templates select="@*[name() != 'name']|*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The first template copies all elements and attributes to the output. The second supersedes the attribute value and copies the sub-elements and other attributes of xs:element
if they exist.
Show the xslt you are using
– Ricardo Pontual
Which criteria you use to select the element that needs to be changed?
– helderdarocha