How to concat a string to xsl:value-of select="...?
Asked Answered
P

6

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

Is there any way to concat another string to

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

I need to pass some text to href attribute in addition to the report property value

Pneumatophore answered 1/5, 2012 at 8:50 Comment(0)
D
169

You can use the rather sensibly named xpath function called concat here

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

Of course, it doesn't have to be text here, it can be another xpath expression to select an element or attribute. And you can have any number of arguments in the concat expression.

Do note, you can make use of Attribute Value Templates (represented by the curly braces) here to simplify your expression

<a href="{concat('myText:', /*/properties/property[@name='report']/@value)}"></a>
Desultory answered 1/5, 2012 at 9:45 Comment(2)
@TimC: Good, but the concat() function isn't necessary here.Rilke
I have the following tag: <a title="" href="/page.cfm?id=425">Anders, John</a> and I would like to create a hidden field in XSLT which only takes the ID #. How can I achieve that?Materialism
C
32

Three Answers :

Simple :

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

Using 'concat' :

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

Attribute shortcut as suggested by @TimC

<img src="{concat(//your/xquery/path,'vmLogo.gif')}" />
Censure answered 20/5, 2013 at 9:37 Comment(1)
As noted by Dimitre, you don't need the concat here: <img src="{//your/xpath}vmLogo.gif" />Perryperryman
R
17

Use:

<a href="wantedText{/*/properties/property[@name='report']/@value)}"></a>
Rilke answered 1/5, 2012 at 12:28 Comment(0)
C
5

The easiest way to concat a static text string to a selected value is to use element.

<a>
  <xsl:attribute name="href"> 
    <xsl:value-of select="/*/properties/property[@name='report']/@value" />
    <xsl:text>staticIconExample.png</xsl:text>
  </xsl:attribute>
</a>
Cinda answered 11/7, 2013 at 7:51 Comment(0)
S
4

Easiest method is

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

when the XML Structure is

<title>The Confidence Man</title>
<author>
  <first-name>Herman</first-name>
  <last-name>Melville</last-name>
</author>
<price>11.99</price>
Saturant answered 18/10, 2017 at 6:9 Comment(0)
C
1

Not the most readable solution, but you can mix the result from a value-of with plain text:

<a>
  <xsl:attribute name="href"> 
    Text<xsl:value-of select="/*/properties/property[@name='report']/@value"/>Text
  </xsl:attribute>
</a>
Cockhorse answered 20/3, 2019 at 22:36 Comment(1)
This has some problems: between XSLT instructions, white space only text nodes are ignored; but now that you have a not white space only text node (' Text'), white space (even new line) will be output.Hippie

© 2022 - 2024 — McMap. All rights reserved.