How do I output line breaks from xsl:value of - xslt

I have this statement
xsl:value-of select="metadata/line1"/
where line1 is in the souce xml is:
Microsoft Windows 7 is installed<br/&gt
The HTML output turns out to be:
Microsoft Windows 7 is installed<br/>
I want it to actually insert the break after the word installed instead of outputting the literal <br/>

Seems that what you like to do is "unescape" some xml content. If I'm right disable-output-escaping should help. Try:
<xsl:value-of select="metadata/line1" disable-output-escaping="yes" />
E.g.: How to unescape XML characters with help of XSLT?

Instead of xsl:select, use xsl:copy-of
It will render the HTML as well
See http://www.w3schools.com/xsl/el_copy-of.asp
You can even try <xsl:value-of select=" " disable-output-escaping="yes" />

Related

xslt find and replace regex-lookarounds

I have a problem with XSLT replace().
XML:
<root>
<title>I am title</title>
<body>
the new formula is:<br/>
the speed test 234 km/h<br>
the weight is 49 kg<br/>
in the 1492 Lorenzo de Medici die.
etc.
<dida>the mass is 56 kn</dida>
</body>
</root>
I must replace all the space after number of measure system.
In PHP I found this regex:
((?<=\d)\s(?=km|kg|kn))
In XSLT I have:
<xsl:template match="//*/text()">
<xsl:value-of select="replace(., '\(\(?\<=\\d\)\\s\(?=km\|kg\|kn\)\)', ' ')"></xsl:variable>
</xsl:template>
The problem is < character!
The common notation for '<' inside a literal string is <
That, however, didn't fix it for my XSLT processor (Kernow, using Saxon 9.1.0.3). As it appeared, it doesn't need all those escapes for parentheses and vertical bars. In addition, the lookarounds didn't work. I was able to solve this using
<xsl:value-of select="replace(., '(\d)\s(km|kg|kn)', '$1!$2')"></xsl:value-of>
(replacing with a '!' for clarity).
There are a few other basic errors in your example which I had to fix first: <br> was not correctly closed, and you mustn't terminate <xsl:value-of .. with </xsl:variable>.

Building custom hyperlink

This is driving me slightly potty!
I have a datasheet webpart and I would like to add a hyperlink to one of the columns to open the item in the popout/modal fashion.
So far I have:
<a><xsl:attribute name="href">
<xsl:value-of select="concat('https://mysite/_layouts/listform.aspx?PageType=4&ListId={listiD}&ID=',#ID,'&ContentTypeID=0x0100B0D8940B0260E54DA1649533F29D58D7')"/>
</xsl:attribute>
<xsl:value-of select="#Title" /></a></td>
(I have edited the above code to remove identifying features)
The error that I am getting is "This Web Part does not have a valid XSLT stylesheet. Error: A semi colon character was expected"
I really don't know what to do to fix this!
Thanks in advance,
MW
This is because of the use of the ampersand & in your statement. It needs to be escaped as & to stop XSTL trying to treat the following characters as an entity.
Try this instead:
<xsl:value-of select="concat('https://mysite/_layouts/listform.aspx?PageType=4&ListId={listiD}&ID=',#ID,'&ContentTypeID=0x0100B0D8940B0260E54DA1649533F29D58D7')"/>

Extracting string parts from data

Need to extra part of a string. Assume I can access "date" and get on the output
21.01.2013
Now I don't want to have the '.2013'. I tried these lines:
<xsl:value-of select="date"/>
<xsl:variable name="bdate">
<xsl:value-of select="date"/>
</xsl:variable>
<p>Birthday: <xsl:copy-of substring($bdate,1,5) /></p><br/>
The first line only works, with all lines any variations of the last line will always throw an error. But there is a solution for it, I'm sure. Can anyone help for it .. how would last line look like?
<xsl:value-of select="substring($bdate,1,5)" />
Should work (XSLT has to be well formed XML)

RSS to XHTML using XSLT - How to remove strange characters?

I'm using XSLT to transform RSS files in XHTML.
In order to create a link I use this block of code:
<!-- language: lang-xml -->
<xsl:for-each select="channel/item">
<h3><xsl:value-of select="title"/></h3>
<xsl:value-of select="description"/>
</xsl:for-each>
But the result comes with some unwanted characters:
<!-- language: lang-html -->
<h3><a href="%0A http://site.com/page.htm%0A ">
What am I doing wrong? Thanks in advance for your help.
It looks like the source has URLEncoded line feeds and some whitespace in it. Leading and trailing whitespace can be stripping using the normalize-space() function. The other stuff may be trickier, depending on how regular it is, and which version of XSLT you're using. If the URLs always end in "%0A ", you could do something like:
substring-before(substring-after(link, 'http'), "%")
This will only work all the time if your URLs are never going to have URLEncoded data in them (which might not be a safe assumption). If you're using XSLT 2.0, something like:
normalize-space(replace(link, '%0A', ''))
might work better.

Prevent whitespaces between two XSLT-elements from being removed by the xslt-processor

my question is a bit different from the other ones..
i got an xsl-code like this:
<xsl:value-of select="..."/> <xsl:value-of select="...">
what i want in my result is:
result_of_select_1 result_of_select_2
what i get is:
result_of_select_1result_of_select_2
how can i prevent this? ( any xsl:output option for example? )
All the other solutions i found were specificly for the same problem but in the XML-Source document and not in the XSLT-document like this one...
btw. a solution like "insert elements instead of the spaces" is not a possible solution for my, because the xslt-code is generated dynamically
thanks in advance
The white space as you have it is insignificant and gets discarded. If that was not the case, every last bit of white space you have in your XSLT code would end up in the result document. You must be explicit about the white space you want in the result.
User either:
<xsl:value-of select="concat(..., ' ', ...)" />
or:
<xsl:value-of select="..." />
<xsl:text> </xsl:text>
<xsl:value-of select="..." />
Use:
<xsl:value-of select="..."/><xsl:text> </xsl:text><xsl:value-of select="...">
EDIT:
Refer to this ASCII table for other symbols