开发者

limit number of characters in a string - XSLT

开发者 https://www.devze.com 2023-03-30 21:06 出处:网络
I am using a third party asp.net control to pull and display the latest content from the database. The control pulls the title of the latest published content using a xsl file. My issue is the title(c

I am using a third party asp.net control to pull and display the latest content from the database. The control pulls the title of the latest published content using a xsl file. My issue is the title(content piece) being too long. They place I used to display has no room for about 100 characters. I need to trim(not the white spaces) end part and may be limit it to a few words or some characters. It uses the xsl file -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <table border="0" cellspacing="0" cellpadding="0" width="100%">
            <xsl:for-each select="Collection/Content">
                <tr>
                    <td>
                        <a>
                            <xsl:attribute name="href">
                                <xsl:choose>
                                    <xsl:when test="Type ='Assets' or Type = 8 ">
                                        javascript:void window.open('showcontent.aspx?id=<xsl:value-of select="ID"/>')
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:value-of select="QuickLink"/>
                                    </xsl:otherwise>
                                </xsl:开发者_运维百科choose>
                            </xsl:attribute>
                            <xsl:value-of select="Title"/>
                        </a>
                    </td>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>

This part, <xsl:value-of select="Title"/> is where you the title is and I need to shorten it.. (putting ... in the end perhaps)

How can I do it? Can I get this done in the xsl file itself without using JQuery? Many thanks,


<xsl:choose>
  <xsl:when test="string-length(Title) > 100">
    <xsl:value-of select="substring(Title, 1, 97)" />...
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="Title" />
  </xsl:otherwise>
</xsl:choose>


You can use the substring(text, startingIndex, length) XSLT function:

<xsl:value-of select="substring(Title, 1, 100)"/>

Note that substring index starts from 1, instead of the usual 0.

More at http://zvon.org/xxl/XSLTreference/Output/xpathFunctionIndex.html

Note also, that .NET does not implements XSLT 2.0, only XSLT 1.0 (hence the above reference: its the list of XSLT 1.0 functions).


Limit the 20 characters

If your code likes this

<xsl:value-of select="EmpBio" disable-output-escaping="yes"/>

Just change with new one written below

<xsl:value-of select="substring(BioData, 1, 20)" disable-output-escaping="yes"/>

Note that the quotation marks are removed from the element name BioData

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号