22
votes

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?

1
It is not clear what you want to do and your code is not syntactically-legal XML. Also, it is not clear where in the code you want to construct an element, whose name is the value of a variable. Please edit + correct.Dimitre Novatchev
As a rule of thumb, any example (and question) should not exceed 15 (20) lines. Also, although we're more than happy to help, please refrain from demanding a solution.phihag
This isn't clear to me, mostly because all those variable declarations that will be lost in scope.user357812
Hello all, I didn't completed/validated the code. I just want to know the way of generating the element name through variable value. I have a task to generate one new element as a wrapper if there is an existence of some set of elements. Based on the availability of the child tags, the element name will differ. For example, if <i> and <b> tags exist then <i_b> need to be generated. If <i>, <b> and <c> exists then <i_b_c> needs to be generated. I think this info will help you...Bhuvana

1 Answers

66
votes

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.