代码之家  ›  专栏  ›  技术社区  ›  Kirk Liemohn

如何使用XSLT将节点集放入属性值?

  •  2
  • Kirk Liemohn  · 技术社区  · 15 年前

    我有一个要调用的xsl模板,以放入HTML节点属性:

    <xsl:variable name="onClickJavaScript">
      <xsl:call-template name="GetOnClickJavaScript" />
    </xsl:variable>
    <a id="123">
      <xsl:attribute name="onclick">
        <xsl:value-of select="$onClickJavaScript" />
      </xsl:attribute>
    </a>
    

    我的问题是调用模板返回一个节点集,而属性值希望是一个字符串。我似乎找不到任何方法将节点集转换为有效的字符串。

    我已尝试将行的值更改为:

    <xsl:value-of select="$onClickJavaScript/text()" />
    

    但它抱怨它需要返回一个节点集,而不是(这正是我所希望的,没有抱怨的部分)。

    在Visual Studio调试器中,我可以看到$onClickJavaScript是一个具有单个维度和项的节点集。我的绳子埋在里面,但不可接近。

    更新。。。

    当我调用模板时,它会经过一个大case语句并调用另一个模板。另一个模板有 <xsl:choose> 在这种情况下,它沿着以下路径:

    <xsl:when test="$isPrimary='true'">         
      <!-- Just provide the onclick event JavaScript -->
      <xsl:value-of select='concat($launchMethod, "(this, &apos;", $id, "&apos;, &apos;", $nodeid, "&apos;, &apos;", $instanceid, "&apos;)")' />
    </xsl:when>
    
    3 回复  |  直到 15 年前
        1
  •  1
  •   Dimitre Novatchev    15 年前

    我的问题是调用模板 返回节点集和属性 值希望是字符串。我不能 似乎找到了改变 节点设置为有效的字符串。

    问题在于你没有显示的代码 .

    事实上,我根本无法重现这个问题。

    这种转变 :

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
        <xsl:variable name="onClickJavaScript">
          <xsl:call-template name="GetOnClickJavaScript" />
        </xsl:variable>
    
       <xsl:variable name="onClickJavaScript2">
          <xsl:call-template name="GetOnClickJavaScript2" />
        </xsl:variable>
    
        <a id="123">
          <xsl:attribute name="onclick">
            <xsl:value-of select="$onClickJavaScript" />
          </xsl:attribute>
        </a>
        <a id="123456">
          <xsl:attribute name="onclick">
            <xsl:value-of select="$onClickJavaScript" />
          </xsl:attribute>
        </a>
     </xsl:template>
    
     <xsl:template name="GetOnClickJavaScript">Hello</xsl:template>
    
     <xsl:template name="GetOnClickJavaScript2">
      <a>Hello</a>
     </xsl:template>
    </xsl:stylesheet>
    

    当应用于任何XML文档(未使用)时,将生成其输出而不会出现任何错误 :

    <a id="123" onclick="Hello"/>
    <a id="123456" onclick="Hello"/>
    
        2
  •  2
  •   Dirk Vollmar    15 年前

    根据提供的信息,您的第一个样本应该可以工作,即。

    <xsl:attribute name="onclick">
      <xsl:value-of select="$onClickJavaScript" />
    </xsl:attribute>
    

    应该插入一个 onclick 属性的(字符串)值为 $onClickJavaScript .

    甚至可以通过使用 attribute value template :

    <a id="123" onclick="{$onClickJavaScript}">...</a>
    

    或者去掉变量并调用属性节点内的模板:

    <xsl:attribute name="onclick">
      <xsl:call-template name="GetOnClickJavaScript" />
    </xsl:attribute>
    
        3
  •  1
  •   carols10cents    15 年前

    我也不能复制这个。。。我试了一下,a上的onclick得到“foo(this,'3','test')”。还有什么不同?您试图调用xsl:value of on的结果树片段是什么?

    <xsl:template match="/">
      <xsl:variable name="onclick-val">
        <xsl:call-template name="foo" />
      </xsl:variable>
      <a>
        <xsl:attribute name="onclick">
          <xsl:value-of select="$onclick-val" />
        </xsl:attribute>
      </a>
    </xsl:template>
    
    <xsl:template name="foo">
      <xsl:choose>
        <xsl:when test="false()">
          false
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="callme" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    
    <xsl:template name="callme">
      <xsl:choose>
        <xsl:when test="true()">
          <xsl:value-of select='concat("foo", "(this, &apos;", "3", "&apos;, &apos;", "test", "&apos;)")' />
        </xsl:when>
        <xsl:otherwise>
          false
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>