开发者

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

开发者 https://www.devze.com 2023-04-12 18:37 出处:网络
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...

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


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:


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.


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().


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'

0

精彩评论

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

关注公众号