简体   繁体   中英

XSLT- how to display the first word of the paragraph as BOLD

I am unable to do this. I am using XML file, which will be converted to HTML using XSLT. The sample XML file would be like this...

<name>ABC</name>
<dob>09-Jan-1973</dob>
..
..
<info>My name is ABC. I am working with XYZ Ltd.

I have an experience of 5 years.

I have been working on Java platform since 5 years.
</info>

The info tag contains information which is in the form of paragraphs. I want the first word bold. Following will be the HTML output, only for info tag..

<b>My</b> name is ABC. I am working with XYZ Ltd.

<b>I</b> have an...

<b>I</b> have been working....

Have a nice day John

Try using Pseudo-Element property in css

p:first-letter
{
font-weight: bold;
}

i would work with the substring methods .., something like this

<xsl:template match="info" mode="parapgrah">
  <b><xsl:value-of select="substring-before(text(), ' ')" /></b><xsl:value-of select="substring-after(text(), ' ')" />
</xsl:template>

ups - this will only replace the first occurence .. you'll need to add a foreach for every 'paragraph'

This transformation (XSLT 2.0):

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="info">
  <xsl:variable name="vParas" select="tokenize(.,'&#xA;')[normalize-space()]"/>

   <xsl:for-each select="$vParas">
    <xsl:variable name="vHead" select=
        "tokenize(., '[\s.?!,;:\-]+')[.][1]"/>
    <b>
      <xsl:sequence select="$vHead"/>
    </b>
    <xsl:sequence select="substring-after(., $vHead)"/>
    <xsl:sequence select="'&#xA;&#xA;'"/>
     </xsl:for-each>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

when applied on the provided input (massaged into a wellformed XML document):

<t>
    <name>ABC</name>
    <dob>09-Jan-1973</dob> .. .. 
    <info>My name is ABC.

    I am working with XYZ Ltd.

    I have an experience of 5 years.

    I have been working on Java platform since 5 years. </info>
</t>

produces the wanted, correct result :

<b>My</b> name is ABC. 

<b>I</b> am working with XYZ Ltd. 

<b>I</b> have an experience of 5 years. 

<b>I</b> have been working on Java platform since 5 years.

And this displays in the browser as expected:


name is ABC. 叫ABC。

am working with XYZ Ltd. 正在与XYZ Ltd.合作。

have an experience of 5 years. 有5年的经验。

have been working on Java platform since 5 years. 已经从事Java平台5年了。


An XSLT 1.0 solution is also possible and is a little bit more complicated.

Explanation : Use of the standard XPath 2.0 function tokenize() and of the standard XPath functions normalize-space() and substring-after() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM