XSLT: changing the values ​​of certain attributes

I am new to XSLT and have a simple task:

Suppose I have the following XML:

<Element1> <Element2 attr1="1"/> </Element1> <Element1 attr1="2"/> <Element1> <Element2 attr1="2"/> </Element1> 

I want to convert XML to the same XML with one change: all attributes with the name "attr1" no matter where they should be converted, so for example, "1" will be "A" and "2" will be "X" ", i.e. e. to

 <Element1> <Element2 attr1="A"/> </Element1> <Element1 attr1="X"/> <Element1> <Element2 attr1="X"/> </Element1> 

How can i achieve this? Thanks in advance!

+7
source share
4 answers

You can define characters to replace and replace characters, then use translate . You can use this XSLT:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output method="xml" indent="yes"/> <xsl:variable name="in">12</xsl:variable> <xsl:variable name="out">AX</xsl:variable> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="@attr1"> <xsl:attribute name="attr1"> <xsl:value-of select="translate(., $in, $out)"/> </xsl:attribute> </xsl:template> </xsl:stylesheet> 

Another way:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output method="xml" indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="@attr1"> <xsl:choose> <xsl:when test=". = '1'"> <xsl:attribute name="attr1"> <xsl:text>A</xsl:text> </xsl:attribute> </xsl:when> <xsl:when test=". = '2'"> <xsl:attribute name="attr1"> <xsl:text>X</xsl:text> </xsl:attribute> </xsl:when> </xsl:choose> </xsl:template> </xsl:stylesheet> 

<xsl:template match="@attr1"> will match all attr1 attributes, and then using xsl:choose you will create the appropriate value for this attribute.

+8
source

You did not say what happens when @attr = 3, for example, so there is another suggestion to simply copy the value if it is not one of the selected ones.

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="@attr1"> <xsl:attribute name="attr1"> <xsl:choose> <xsl:when test=". = 1"> <xsl:text>A</xsl:text> </xsl:when> <xsl:when test=". = 2"> <xsl:text>X</xsl:text> </xsl:when> <xsl:otherwise> <xsl:value-of select="." /> </xsl:otherwise> </xsl:choose> </xsl:attribute> </xsl:template> </xsl:stylesheet> 
+8
source

Another way using the document function:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:l="local" > <xsl:output method="xml" indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="@attr1"> <xsl:attribute name="attr1"> <xsl:value-of select="document('')//l:item[l:in = current()]/l:out"/> </xsl:attribute> </xsl:template> <xml xmlns="local"> <item> <in>1</in> <out>A</out> </item> <item> <in>2</in> <out>X</out> </item> </xml> </xsl:stylesheet> 
+2
source

xslt 2 version below works:

  <xsl:output method="xml" indent="yes"/> <xsl:template match="node() | @*"> <xsl:copy> <xsl:apply-templates select="node() | @*"/> </xsl:copy> </xsl:template> <xsl:template match="@attr1[.='1']"> <xsl:attribute name="attr1"> <xsl:value-of select="replace(.,'1','A')"/> </xsl:attribute> </xsl:template> <xsl:template match="@attr1[.='2']"> <xsl:attribute name="attr1"> <xsl:value-of select="replace(.,'2','X')"/> </xsl:attribute> </xsl:template> </xsl:stylesheet> 
0
source

All Articles