ChatGPT解决这个技术问题 Extra ChatGPT

如何将字符串连接到 xsl:value-of select="...?

<a>
    <xsl:attribute name="href"> 
     <xsl:value-of select="/*/properties/property[@name='report']/@value" />
    </xsl:attribute>
</a>    

有没有办法将另一个字符串连接到

<xsl:value-of select="/*/properties/property[@name='report']/@value"  />

除了报告属性值之外,我还需要将一些文本传递给 href 属性


T
Tim C

您可以在此处使用名为 concat 的命名相当合理的 xpath 函数

<a>
   <xsl:attribute name="href">
      <xsl:value-of select="concat('myText:', /*/properties/property[@name='report']/@value)" />
   </xsl:attribute>
</a>  

当然,这里不一定非得是文本,可以是另一个xpath表达式来选择一个元素或属性。您可以在 concat 表达式中包含任意数量的参数。

请注意,您可以在此处使用属性值模板(由大括号表示)来简化您的表达式

<a href="{concat('myText:', /*/properties/property[@name='report']/@value)}"></a>

@TimC:很好,但这里不需要 concat() 函数。
我有以下标签:<a title="" href="/page.cfm?id=425">Anders, John</a>,我想在 XSLT 中创建一个隐藏字段,它只接受 ID #。我怎样才能做到这一点?
N
Ninjanoel

三个答案:

简单的 :

<img>
    <xsl:attribute name="src">
        <xsl:value-of select="//your/xquery/path"/>
        <xsl:value-of select="'vmLogo.gif'"/>
    </xsl:attribute>
</img>

使用“连接”:

<img>
    <xsl:attribute name="src">
        <xsl:value-of select="concat(//your/xquery/path,'vmLogo.gif')"/>                    
    </xsl:attribute>
</img>

@TimC 建议的属性快捷方式

<img src="{concat(//your/xquery/path,'vmLogo.gif')}" />

正如 Dimitre 所指出的,您在这里不需要 concat:<img src="{//your/xpath}vmLogo.gif" />
D
Dimitre Novatchev

利用:

<a href="wantedText{/*/properties/property[@name='report']/@value)}"></a>

D
DavidKahnt

将静态文本字符串连接到选定值的最简单方法是使用 element.

<a>
  <xsl:attribute name="href"> 
    <xsl:value-of select="/*/properties/property[@name='report']/@value" />
    <xsl:text>staticIconExample.png</xsl:text>
  </xsl:attribute>
</a>

M
Marshall

最简单的方法是

  <TD>
    <xsl:value-of select="concat(//author/first-name,' ',//author/last-name)"/>
  </TD>

当 XML 结构为

<title>The Confidence Man</title>
<author>
  <first-name>Herman</first-name>
  <last-name>Melville</last-name>
</author>
<price>11.99</price>

K
Khin

不是最易读的解决方案,但您可以将 value-of 的结果与纯文本混合:

<a>
  <xsl:attribute name="href"> 
    Text<xsl:value-of select="/*/properties/property[@name='report']/@value"/>Text
  </xsl:attribute>
</a>

这有一些问题:在 XSLT 指令之间,只有空格的文本节点被忽略;但是现在您有一个非空白文本节点(' Text'),将输出空白(甚至是新行)。

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅