how to create a tag by traversing attributes from another tag in xslt

input xml :\[code\]<content> <link> <text>sign on</text> <trackableReference local="/PUBLIC_WEB_URL" title=""/> </link></content>\[/code\]output xml:\[code\]<content> <a id="mylink" href="http://stackoverflow.com/questions/10925758/PUBLIC_WEB_URL" title="">sign on</a></content>\[/code\]xslt's tried which works fine for this:1) \[code\]<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="content"> <content> <xsl:apply-templates/> </content> </xsl:template> <xsl:template match="*[name()='link']"> <a id="mylink"> <xsl:attribute name="href"><xsl:value-of select="trackableReference/@local"/></xsl:attribute> <xsl:attribute name="title"><xsl:value-of select="trackableReference/@title"/></xsl:attribute> <xsl:value-of select="text"/> </a> </xsl:template></xsl:stylesheet>\[/code\]2)\[code\]<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="content"> <content> <xsl:apply-templates/> </content> </xsl:template> <xsl:template match="*[name()='link']"> <a id="mylink"> <xsl:attribute name="href"> <xsl:for-each select="child::*"> <xsl:choose> <xsl:when test="name()='trackableReference'"> <xsl:value-of select="@local"/> </xsl:when> </xsl:choose> </xsl:for-each> </xsl:attribute> <xsl:attribute name="title"> <xsl:for-each select="child::*"> <xsl:choose> <xsl:when test="name()='trackableReference'"> <xsl:value-of select="@title"/> </xsl:when> </xsl:choose> </xsl:for-each> </xsl:attribute> <xsl:for-each select="child::*"> <xsl:choose> <xsl:when test="name()='text'"> <xsl:value-of select="."/> </xsl:when> </xsl:choose> </xsl:for-each> </a> </xsl:template></xsl:stylesheet>\[/code\]But I have a scenario where I have to create some global variables in template whose values are to be dynamically modified according to the child nodes traversed. Example consider above the template modified in this scenario\[code\]<xsl:template match="*[name()='link']"> <xsl:variable name="x1"/> <xsl:variable name="x2"/> <xsl:variable name="x3"/> <xsl:for-each select="child::*"> <xsl:choose> <xsl:when test="name()='trackableReference'"> <xsl:value-of select="@local"/> <xsl:value-of select="@title"/> </xsl:when> <xsl:when test="name()='text'"> <xsl:value-of select="."/> </xsl:when> </xsl:choose> </xsl:for-each> <a id="mylink"> <xsl:attribute name="href"><xsl:value-of select="$x1"/></xsl:attribute> <xsl:attribute name="title"><xsl:value-of select="$x2"/></xsl:attribute> <xsl:value-of select="$x3"/> </a></xsl:template>\[/code\]Here
  • \[code\]x1\[/code\] variable should be assiged with value selected from \[code\]@local\[/code\],
  • \[code\]x2\[/code\] variable should be assiged with value selected from \[code\]@title\[/code\],
  • \[code\]x3\[/code\] variable should be assiged with value selected from \[code\]'text'\[/code\] node.
So I would like to get these variables declared on top to be assigned with values extracted from child nodes traversed. I got stuck here and cannot move forward.Can anyone resolve this problem.
 
Back
Top