【问题标题】:Need variable value as element name using XSLT需要变量值作为使用 XSLT 的元素名称
【发布时间】:2011-06-24 03:30:59
【问题描述】:

我正在将一种 XML 格式转换为另一种格式,我需要插入一个元素并用变量的值命名它。例如,我正在使用 XSLT 尝试以下语句,但我从处理器收到错误消息,指出元素名称无效。

<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>

如何使用 XSLT 变量输出元素名称?

【问题讨论】:

  • 不清楚你想做什么,你的代码在语法上不是合法的 XML。此外,不清楚要在代码中的哪个位置构造一个元素,其名称是变量的值。请编辑+更正。
  • 根据经验,任何示例(和问题)都不应超过 15 (20) 行。此外,尽管我们非常乐意提供帮助,但请不要要求提供解决方案。
  • 这对我来说不是很清楚,主要是因为所有这些变量声明都会在范围内丢失。
  • 大家好,我没有完成/验证代码。我只是想知道通过变量值生成元素名称的方式。如果存在某些元素集,我的任务是生成一个新元素作为包装器。根据子标签的可用性,元素名称会有所不同。例如,如果存在 标签,则需要生成 。如果 存在,则需要生成 。我认为这些信息会对您有所帮助...

标签: xslt xslt-1.0 xslt-2.0


【解决方案1】:

完全可以构造一个名称由变量值动态定义的元素

<xsl:element name="{$vVar}">3</xsl:element>

如果变量$vVar的字符串值恰好是"Hello",那么上面会产生:

<Hello>3</Hello>

但是,如果变量 $vVar 的字符串值恰好是 "123",则会引发错误,因为字符串 "123" 在 XML 中不是合法名称。。 p>

不清楚您希望在代码中的哪个位置动态构造元素(并且代码中还有其他错误),但只需使用上述规则/示例,您将完全按照描述构造元素。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 2012-04-23
  • 1970-01-01
  • 2019-10-01
  • 1970-01-01
相关资源
最近更新 更多