XSLT. Is there a way to add attributes added using <xsl: attribute>?

A simplified example:

<xsl:template name="helper"> <xsl:attribute name="myattr">first calculated value</xsl:attribute> </xsl:template> <xsl:template match="/> <myelem> <xsl:call-template name="helper" /> <xsl:attribute name="myattr">second calculated value</xsl:attribute> </myelem> </xsl:template> 

Is there a way for the second to add a second computed value to the same attribute myattr as a result of node?

I saw that you can use the attribute value template if the target attribute is in the original xml, but can I somehow refer to the attribute value that I previously added to the node result?

Thanks in advance!

+6
source share
4 answers

One approach you can take is to add a parameter to your helper template, which you add to the attribute value.

 <xsl:template name="helper"> <xsl:param name="extra" /> <xsl:attribute name="myattr">first calculated value<xsl:value-of select="$extra" /></xsl:attribute> </xsl:template> 

Then you can simply pass in the second value of the calculation as a parameter

 <xsl:template match="/> <myelem> <xsl:call-template name="helper"> <xsl:with-param name="extra">second calculated value</xsl:with-param> </xsl:call-template> </myelem> </xsl:template> 

However, you do not need to set a parameter with each call. If you do not want to add anything, just name the auxiliary template without parameters and do not add anything to the first calculated value.

+4
source

The simplest approach would be to change the nesting of the bits - let helper just generate text nodes and put <xsl:attribute> in the calling template:

 <xsl:template name="helper"> <xsl:text>first calculated value</xsl:text> </xsl:template> <xsl:template match="/> <myelem> <xsl:attribute name="myattr"> <xsl:call-template name="helper" /> <xsl:text>second calculated value</xsl:text> </xsl:attribute> </myelem> </xsl:template> 

This will set myattr to "first calculated values ​​of the calculated value", if you need a space between the "value" and the "second", you must include this inside one of the <xsl:text> elements

  <xsl:text> second calculated value</xsl:text> 
+2
source

Try the following:

  <xsl:template name="helper"> <xsl:attribute name="myattr">first calculated value</xsl:attribute> </xsl:template> <xsl:template match="/"> <myelem> <xsl:call-template name="helper" /> <xsl:variable name="temp" select="@myattr"/> <xsl:attribute name="myattr"> <xsl:value-of select="concat($temp, 'second calculated value')" /> </xsl:attribute> </myelem> </xsl:template> 
0
source

While this is more or less the same, I would prefer a more concise way of creating a variable rather than having a helper template. Note that you can still call the template from the xsl: variable for a more complex case.

 <xsl:template match="/"> <myelem> <xsl:variable name="first">first calculated value </xsl:variable > <xsl:attribute name="myattr"> <xsl:value-of select="concat($first, 'second calculated value')"/> </xsl:attribute> </myelem> </xsl:template> 
0
source

Source: https://habr.com/ru/post/954756/


All Articles