Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need variable value as element name using XSLT

Tags:

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?

like image 477
Bhuvana Avatar asked Feb 09 '11 13:02

Bhuvana


People also ask

How do you set a variable value in XSLT?

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!

Which symbol is used to get the element value in XSLT?

XSLT <xsl:value-of> Element The <xsl:value-of> element is used to extract the value of a selected node.

What is difference between Param and variable in XSLT?

The difference is that the value of an xsl:param could be set outside the context in which it is declared.


1 Answers

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.

like image 66
Dimitre Novatchev Avatar answered Sep 28 '22 02:09

Dimitre Novatchev