2

I have a requirement that I need to handle B (Bold), I (Italic) and U (underline) tags. We are using XSLT and XPath.

I have an RTF field named <text>......</text>.

I entered some text in this RTF field and made it Bold and Italic, the source xml for the bold and italic is as follows:

<text>
    <strong xmlns="http://www.w3.org/1999/xhtml">adasdasd</strong>
</text>

<text>
    <em xmlns="http://www.w3.org/1999/xhtml">adsadas</em>
</text>

I handled both Bold and Italic tag in this following way.

<xsl:template name="format" match="*[local-name()='strong'] | *[local-name()='em']">
    <xsl:variable name="tagName" select="name()" />
    <xsl:element name="{$tagName}" namespace="{$nSpace}">
        <xsl:copy-of select="@*" />
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>

In the published file I'm getting the tag element as expected

<text><strong>adasdasd</strong></text>

<text><em>adsadas</em></text>

It is working fine for Bold and Italic.
But, I tried 4 ways for underline.

  1. Enter consecutive text in that RTF field
<text>Good Morning Have a nice <span xmlns="http://www.w3.org/1999/xhtml" 
style="text-decoration: underline;">Day</span></text>
  1. Enter a single word
<text>
    <div xmlns="http://www.w3.org/1999/xhtml" style="text-decoration: underline;">Welcome</div>
</text>
  1. Enter text and press ENTER Key
<text>
    <p xmlns="http://www.w3.org/1999/xhtml">jhoiohad</p>
    <p xmlns="http://www.w3.org/1999/xhtml">asuhdoahsoid</p>
    <p xmlns="http://www.w3.org/1999/xhtml" style="text-decoration: underline;">ahsdhaoiodia</p>
    <p xmlns="http://www.w3.org/1999/xhtml">asdasd</p>
</text>
  1. Enter text and press SHIFT + ENTER key
<text>
    huiuiioasiodio
    <br xmlns="http://www.w3.org/1999/xhtml">
    iohoshiofhiohsd
    <br xmlns="http://www.w3.org/1999/xhtml">
    <span xmlns="http://www.w3.org/1999/xhtml" style="text-decoration: underline;">iosiohfioio</span>
    <br xmlns="http://www.w3.org/1999/xhtml">
    uisuidfuiuisdhf
</text>

I don't know how to handle for underline tag. Because different tag elements are generated. In the published file I need to get like this:

<text><u>...</u></text><br>

How to achieve this?

  • What tags are generated in published files? please give some examples. – Huston Lopes Jun 3 '15 at 14:12
  • The <u> element was deprecated in HTML 4.01. (the <u> element was used to define underlined text). – Bjørn van Dommelen Jun 3 '15 at 20:16
  • @Huston Lopes - Only text element and its value is generated – vishnu vardan Jun 4 '15 at 4:58
  • @Huston Lopes - (eg):- <text>some text here...........</text>. Because I'm not handling it in the xslt level...It should not affect other tags like <strong>, <em> etc... – vishnu vardan Jun 4 '15 at 5:01
2

The rich text field editor doesn't generate <u> tags automatically (see my comment about its deprecation). If you want your output to contain these tags there are a few possible approaches; each with specific drawbacks:

  1. Use a plain text field instead of a rich text field.
    Rich text fields are NOT the general solution for everything HTML. They are specifically designed as lightweight content-with-markup blocks supporting simple features only.

  2. Edit the rich text field content via the "source" tab of the rich text field. You will need to explicitly allow the u tag by amending your own template:
    match="*[local-name()='strong'] | *[local-name()='em'] | *[local-name()='u']"

  3. Convert the editor generated "underline" solution to the desired solution. You will need to match any tags (I would limit it to p or span) with a style="text-decoration: underline;" attribute and convert them into a u tag.

Option 1 is the most flexible and least costly solution but also the least user friendly.
Option 2 is much less flexible but equally cheap and slightly more user friendly.
Option 3 is the least flexible and most expensive solution but it is by far the user friendliest (users don't have to touch any HTML code).

The main drawback of option 3 is that with the next release of SDL Web the generated HTML for underline text could be radically different breaking this solution. It will most likely need constant maintenance and has the most unforeseen side-effects.

  • Rich text is "XML in the XHTML namespace," to quote @DominicCronin. Someone can check, but #2 might not work (will the CME let you save u tags?). For conversions from XML to HTML4, a Template Building Block might combine the best of option #3 and letting you handle side-effects in some modular piece of code, without affecting the raw (XML) content. – Alvin Reyes Jun 7 '15 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.