开发者

Need variable value as element name using XSLT

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>开发者_如何学Python;
      <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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜