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" />
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.
<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>
Im useing this aproach to create an image while in an xsl transformer:
XSLT: Convert base64 data into image files
My issue is that now i what to create an IMG tag that references the created image.
Here is what I've done:
<!-- CREATE TEMP IMAGE -->
<xsl:variable name="b64" select="b64:new(string(./swe:Text/swe:value))"/>
<xsl:variable name="fos" select="fos:new(string('./temp.jpeg'))"/>
<xsl:value-of select="fos:write($fos, b64:getBinaryValue($b64))"/>
<xsl:value-of select="fos:close($fos)"/>
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('./temp', '.jpeg')"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="concat('./temp', '.jpeg')"/>
</xsl:attribute>
<xsl:attribute name="align">TOP</xsl:attribute>
<xsl:attribute name="width">100</xsl:attribute>
<xsl:attribute name="height">75</xsl:attribute>
</img>
</a>
Im taking the output from above and displaying it in a JTextPane,
but the image is always broken.
I imagine it's an issue with relative pathing,
but i dont know how to get the absolute path from within the xsl.
EDIT:
Ok, turns out that i cant reference images relative in the img tag because JTextPanes cant handle that. I was able to get this working using the following:
<xsl:variable name="b64" select="b64:new(string(./swe:Text/swe:value))"/>
<xsl:variable name="fos" select="fos:new(string(concat('/temp/', #name, '.jpeg')))"/>
<xsl:value-of select="fos:write($fos, b64:getBinaryValue($b64))"/>
<xsl:value-of select="fos:close($fos)"/>
<img>
<xsl:attribute name="src">
<xsl:value-of select="concat('file:///C:/temp/', #name, '.jpeg')"/>
</xsl:attribute>
<xsl:attribute name="align">MIDDLE</xsl:attribute>
<xsl:attribute name="width">200</xsl:attribute>
<xsl:attribute name="height">150</xsl:attribute>
</img>
but i really need it to either be relative or
i need to get access to the system defined temp directory.
Anybody know how to this?
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>
Can i use a Select within a concat in xslt?
eg
<xsl:for-each select="root/OrderItems/lineitem">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="concat('http://www.site.com/r&h=11', '&q=',<xsl:value-of select="Quantity" />, )" />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
Try this:
<xsl:for-each select="root/OrderItems/lineitem">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of
select="concat('http://www.site.com/r&h=11', '&q=', Quantity)" />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
No, because it is not well formed XML, you cannot put a self closing XML element within a self closing XML element, or I suppose in this case you cannot use an XML element in the value of an XML attribute