Andrew Truckle
Andrew Truckle

Reputation: 19107

Finding the number of characters in a text string up to a certain character

I have some notes in my XML document. Each note follows this rule:

NOTE1
NOTE1<div>NOTE2</div>
<div>NOTE2</div>

In my XML I have:

<MeetingWorkBook>
   <Labels>
      <Note>Note</Note>
   </Labels>
   <Meeting>
      <Note>Note1</Note>
   </Meeting>
   <Meeting>
      <Note>Note1&lt;div&gt;Note2&lt;/div&gt;</Note>
   </Meeting>
   <Meeting>
      <Note>&lt;div&gt;Note2&lt;/div&gt;</Note>
   </Meeting>
</MeetingWorkBook>

In the XSL file I have:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" version="4.01"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
        doctype-public="//W3C//DTD XHTML 1.0 Transitional//EN"/>
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
                <link rel="stylesheet" type="text/css" href="Workbook-off.css"/>
                <title>Title</title>
            </head>
            <body>
                <xsl:for-each select="MeetingWorkBook/Meeting">
                    <table>
                        <xsl:if test="normalize-space(Note) != ''">
                            <tr>
                                <td class="borderDotMeetingNotes" colspan="4">
                                    <xsl-if test="normalize-space(substring-before(Note, '&lt;div&gt;') != '')">
                                        <xsl:value-of select="//Labels/Note"/>:&#160;
                                    </xsl-if>
                                    <xsl:value-of select="Note" disable-output-escaping="yes"/>
                                </td>
                            </tr>
                        </xsl:if>
                    </table>            
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

What am I trying to achieve? In the final output, if there is a NOTE1 then I want to display the "Note: " label prefix. That is all.

The third example fails because there is no NOTE1. This is harder to explain than it really is. :(

I have tried:

<xsl:if test="normalize-space(Note) != ''">
    <tr>
        <td class="borderDotMeetingNotes" colspan="4">
           <xsl-if test="normalize-space(substring-before(Note, '&lt;div&gt;') != '')">
                <xsl:value-of select="//Labels/Note"/>:&#160;
           </xsl-if>
           <xsl:value-of select="Note" disable-output-escaping="yes"/>
        </td>
    </tr>
</xsl:if>   

But "Note:" still appears.

Upvotes: 2

Views: 72

Answers (2)

Andrew Truckle
Andrew Truckle

Reputation: 19107

Thanks to the answers already provided and subsequent comments.

It seems that using normalize-space still confused things and in the end I did this which appears to be fully operational:

<xsl:if test="normalize-space(Note) != ''">
    <tr>
        <td class="borderDotMeetingNotes" colspan="4">
            <xsl:if test="normalize-space(substring-before(Note, '&lt;div')) or not(contains(Note, '&lt;div'))">
                <xsl:value-of select="//Labels/Note"/>:&#160;
            </xsl:if>
            <xsl:value-of select="Note" disable-output-escaping="yes"/>
        </td>
    </tr>
</xsl:if>

Note that in the end I also only searched for <div since the user would actually have a class associated with the div.

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 116992

If I understand correctly (which is not at all certain) you want to make your test:

<xsl:if test="substring-before(Note, '&lt;div&gt;')">
    <xsl:value-of select="//Labels/Note"/>:&#160;
</xsl:if>

Note: you need to use <xsl:if>, not <xsl-if>.

Upvotes: 1

Related Questions