I am converting one format of XML to another and I need to insert an element and name it with the value of a variable. For example, I am trying the statements below using XSLT, but I am getting an error from the processor saying that the element name is invalid.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="no" omit-xml-declaration="no"/> <xsl:variable name="i" select="i"/> <xsl:variable name="b" select="b"/> <xsl:variable name="u" select="u"/> <xsl:variable name="s" select="s"/> <xsl:variable name="r" select="r"/> <xsl:variable name="m" select="m"/> <xsl:variable name="n" select="n"/> <xsl:template match="/|comment()|processing-instruction()"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="*"> <xsl:element name="{local-name()}"> <xsl:apply-templates select="@*|node()"/> </xsl:element> </xsl:template> <xsl:template match="@*"> <xsl:attribute name="{local-name()}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:template> <xsl:template match="//w:r"> <xsl:if test="not(./descendant::w:i)"> <xsl:variable name="i">0</xsl:variable> </xsl:if> <xsl:if test="not(./descendant::w:b)"> <xsl:variable name="b">0</xsl:variable> </xsl:if> <xsl:if test="not(./descendant::w:u)"> <xsl:variable name="u">0</xsl:variable> </xsl:if> <xsl:if test="not(./descendant::w:caps)"> <xsl:variable name="c">0</xsl:variable> </xsl:if> <xsl:if test="not(contains($i,'0'))"> <xsl:variable name="r" select="$i"></xsl:variable> <xsl:text>KARTHIKK</xsl:text> </xsl:if> <xsl:if test="not(contains($b,'0'))"> <xsl:variable name="m" select="$r+$b"></xsl:variable> </xsl:if> <xsl:if test="not(contains($u,'0'))"> <xsl:variable name="n" select="$m+$u"></xsl:variable> </xsl:if> <xsl:copy namespaces="no"><xsl:apply-templates/></xsl:copy> <!-- <xsl:element name="{local-name(.)}"><xsl:element><xsl:value-of select="$n"/><xsl:apply-templates/></xsl:element></xsl:element>--> </xsl:template> </xsl:stylesheet>
How can I output an element name using a XSLT variable?
XSLT <xsl:variable> The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!
XSLT <xsl:value-of> Element The <xsl:value-of> element is used to extract the value of a selected node.
The difference is that the value of an xsl:param could be set outside the context in which it is declared.
It is perfectly possible to construct an element whose name is defined dynamically by the value of a variable:
<xsl:element name="{$vVar}">3</xsl:element>
If the string value of the variable $vVar
happens to be "Hello"
, then the above produces:
<Hello>3</Hello>
However, if the string value of the variable $vVar
happens to be "123"
then an error is raised as the string "123"
isn't a legal name in XML.
It isn't clear where in your code you'd like to construct an element dynamically (and there are other errors in the code), but just use the above rules/example and you will construct elements exactly as described.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With