<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
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
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')}" />
Use:
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>
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>
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>
Related
I have a simple xsl file:
<xsl:for-each select="part">
<div class="part">
<h2>Part Name:<xsl:value-of select="#partName" /></h2>
<xsl:for-each select="input">
<p>
<input>
<xsl:attribute name="value">
<xsl:value-of select="." />
</xsl:attribute>
<xsl:attribute name="name"><xsl:value-of select="#inputName" /></xsl:attribute>
</input>
</p>
</xsl:for-each>
</div>
</xsl:for-each>
I want to save partname and use it latter. I tried as the following:
<xsl:for-each select="part">
<div class="part">
<h2>Part Name:<xsl:value-of select="#partName" /></h2>
**<xsl:param name="part_name"><xsl:value-of select="#partName" /></xsl:param>**
<xsl:for-each select="input">
<p>
<input>
<xsl:attribute name="value">
<xsl:value-of select="." />
</xsl:attribute>
<xsl:attribute name="name">**<xsl:value-of select="$part_name" />**-<xsl:value-of select="#inputName" /></xsl:attribute>
</input>
</p>
</xsl:for-each>
</div>
</xsl:for-each>
but it doesn't work. I don't know what should do.
You are probably getting the error Keyword xsl:param may not be used here.. This is because xsl:param should only really be found under the root xsl:stylesheet element, and are used for passing values from the external application.
What you need is xsl:variable in this case
<xsl:variable name="part_name">
<xsl:value-of select="#partName"/>
</xsl:variable>
Or simpler still....
<xsl:variable name="part_name" select="#partName"/>
In fact, you could do away with the variable altogether. Instead of doing this
<xsl:value-of select="$part_name"/>
You could just do this, to get the value from the parent element
<xsl:value-of select="../#part_name"/>
Do bear in mind, if you do use a variable, it will only be local to the scope of the xsl:for-each in this case.
I have an xml with img tag
<img>
source
</img>
I want to generate:
<img src="source.jpg">
I tried something like that:
<img>
<xsl:attribute name="src">
<xsl:text>
<xsl:value-of select="node()" />.jpg
</xsl:text>
</xsl:attribute>
</img>
but it doesng work
Use:
<img src="{normalize-space()}.jpg"/>
This assumes the <img> element is the current node.
The reason why what you are doing does not work is that you cannot evaluate XSLT expressions inside of the <xsl:text> element.
<xsl:text> can only contain literal text, entity references, and #PCDATA.
If you move the <xsl:value-of> outside of the <xsl:text>, then the following will work:
<img>
<xsl:attribute name="src">
<xsl:value-of select="node()" />
<xsl:text>.jpg</xsl:text>
</xsl:attribute>
</img>
However, selecting <xsl:value-of select="node()> for the <img> in your example will include the carriage returns and whitespace characters inside of the <img> element, which is probably not what you want in your src attribute value.
That is why Dimitre Novatchev used normalize-space() in his answer. Applying that to the example above:
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(node())" />
<xsl:text>.jpg</xsl:text>
</xsl:attribute>
</img>
If you get rid of the <xsl:text> as Fabiano's solution suggests, then you could also do this:
<img>
<xsl:attribute name="src"><xsl:value-of select="normalize-space(node())" />.jpg</xsl:attribute>
</img>
Just remove the tag xsl:text, in this case, you won't need it. Try this:
<img>
<xsl:attribute name="src">
<xsl:value-of select="concat(node(), '.jpg')"/>
</xsl:attribute>
</img>
I didn't test it, but it should work. =)
<img>
<xsl:attribute name="src">
<xsl:value-of select="my_photo/#href" />
</xsl:attribute>
</img>
<my_photo href="folder/poster.jpg" />
I am transforming an XML document. There is an attribute #prettydate that is a string similar to "Friday, May 7, 2010". I want to split that string and add links to the month and the year. I am using the exslt:strings module and I can add any other necessary EXSLT module.
This is my code so far:
<xsl:template match="//calendar">
<xsl:variable name="prettyparts">
<xsl:value-of select="str:split(#prettydate,', ')"/>
</xsl:variable>
<table class='day'>
<thead>
<caption><xsl:value-of select="$prettyparts[1]"/>,
<a>
<xsl:attribute name='href'><xsl:value-of select="$baseref"/>?date=<xsl:value-of select="#highlight"/>&per=m</xsl:attribute>
<xsl:value-of select='$prettyparts[2]'/>
</a>
<xsl:value-of select='$prettyparts[3]'/>,
<a>
<xsl:attribute name='href'><xsl:value-of select="$baseref"/>?date=<xsl:value-of select="#highlight"/>&per=y</xsl:attribute>
<xsl:value-of select='$prettyparts[4]'/>
</a>
</caption>
<!--etcetera-->
I have verified, by running $prettyparts through a <xml:for-each/> that I am getting the expected nodeset:
<token>Friday</token>
<token>May</token>
<token>7</token>
<token>2010</token>
But no matter which way I attempt to refer to a particular <token> directly (not in a foreach) I get nothing or various errors to do with invalid types. Here's some of the syntax I've tried:
<xsl:value-of select="$prettyparts[2]"/>
<xsl:value-of select="$prettyparts/token[2]"/>
<xsl:value-of select="exsl:node-set($prettyparts/token[2])"/>
<xsl:value-of select="exsl:node-set($prettyparts/token)[2]"/>
Any idea what the expression ought to be?
ETA: Thanks to #DevNull's suggestion, the correct expression is:
<xsl:value-of select="exsl:node-set($prettyparts)[position()=2]"/>
and, I have to set the variable this way:
<xsl:variable name="prettyparts" select="str:split(#prettydate,', ')" />
Try using [position()=2] instead of [2] in your predicates.
Example:
<xsl:value-of select="$prettyparts[position()=2]"/>
Is it possible to add text from a xsl variable to the inside of a html tag something like this?
<xsl:variable name="selected">
<xsl:if test="#class = 'selected'"> class="selected"</xsl:if>
</xsl:variable>
<li{$selected}></li>
Try this:
<xsl:element name="li">
<xsl:if test="#class = 'selected'">
<xsl:attribute name="class">
selected
</xsl:attribute>
</xsl:if>
</xsl:element>
Optionally, the xsl:if could be nested in the xsl:attribute, instead of the other way around, if a class="" is desired. As already mentioned, it is unwise to write this as literal text.
You should not attempt to write this as literal text, instead look at xsl:element and xsl:attribute. Rough example:
<xsl:element name="li">
<xsl:attribute name="class">
<xsl:value-of select="$selected" />
</xsl:attribute>
</xsl:element>
Full documentation here.
Note that if you are careful enough to make it its first child, you can use <xsl:attribute> directly inside of your <li> tag, instead of using <xsl:element>
<li>
<xsl:if test="$selected">
<!-- Will refer to the last opened element, li -->
<xsl:attribute name="class">selected</xsl:attribute>
</xsl:if>
<!-- Anything else must come _after_ xsl:attribute -->
</li>
<a>
<xsl:attribute name="href">
<xsl:value-of select="link" />
</xsl:attribute>
<xsl:attribute name="target">new</xsl:attribute>
<xsl:value-of select="title" />
</a>
Thats my template, and in my code:
sb.Append("<title>");
sb.AppendFormat("{0} - {1}", f.UserName, f.PointTypeDesc);
sb.Append("</title>");
sb.Append("<link>");
sb.AppendFormat("{0}", HttpUtility.UrlEncode(url));
sb.Append("</link>");
url is "http://www.cnn.com"
But it renders as: "http://localhost/http://www.cnn.com"
any ideas?
It seems to me that the problem must be with HttpUtility.UrlEncode. Have you checked the contents of the xml that you're creating? The XSL looks correct to me, although it could be written more tersely as:
<a href="{#link}" target="new">
<xsl:value-of select="title"/>
</a>