xsl-list
[Top] [All Lists]

AW: Preserving inline elements when using string functions

2003-08-23 08:41:30
Hi Brook,

in XML one doesn't think of "inline elements in strings" but of
"a string followed by an element followed by another string".

In your example, do the LF processing only on strings, i.e. only on:
            foo
            <xsl:value-of select="."/>
            bar

You didn't show us how you pass the output of the link template
to the paragraphs template. Are you using two templates in a pipeline?
Or are you using node-set functions?

Show us, how you contruct the string and pass it to the paragraphs
template then someone will tell what's wrong with the code.
My first guess is you are using a xsl:for-each loop with xsl:value-of
to eleminate the "inline" elements.

Regards,
Markus
__________________________
Markus Abt
Comet Computer GmbH
http://www.comet.de


----------
Von:    Brook Ellingwood
Gesendet:       Samstag, 23. August 2003 02:35
An:     xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Betreff:        [xsl] Preserving inline elements when using string functions

Hi,

I'm trying to figure out how to preserve inline elements in a string when it
gets passed through a template using string functions to manipulate the
content. 

This is my XML:

    <bodytext>This is the <link url="#">link</link>
              This is another line of text</bodytext>

My first template formats the "link" element into an HTML "a" element, using
the "url" attribute as the HTML "href" attribute. I've broken the lines out
for ease of reading in e-mail):

    <xsl:template name="embeddedLinks">
        <xsl:apply-templates select="./node()"/>
    </xsl:template>

    <xsl:template match="link">
            <a style="text-decoration:underline">
            <xsl:attribute name="href">
                <xsl:value-of select="@url"/>
            </xsl:attribute>
            foo
            <xsl:value-of select="."/>
            bar
        </a>
    </xsl:template>

The output of that template is then passed to another template that finds
all the CR/LF codes in the string and puts "DIV" tags around each line.

<xsl:template name="paragraphs">
   <xsl:param name="string" />
   <xsl:param name="linebreak" select="'&#xA;'" />
   <xsl:param name="divClass" />
   <xsl:choose>
      <xsl:when test="contains($string, $linebreak)">
         <DIV><xsl:attribute name="class"><xsl:value-of select="$divClass"
/></xsl:attribute> 
             <xsl:value-of select="substring-before($string, $linebreak)" />
         </DIV>
         <xsl:call-template name="paragraphs">
            <xsl:with-param name="string"
                            select="substring-after($string, $linebreak)" />
            <xsl:with-param name="linebreak" select="$linebreak" />
            <xsl:with-param name="divClass" select="$divClass" />
         </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
         <DIV><xsl:attribute name="class"><xsl:value-of select="$divClass"
/></xsl:attribute>
             <xsl:value-of select="$string" />
            </DIV>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template> 

Individually, they work great, but in combination this is what I get for my
output:

    <DIV class="">This is the foolinkbar</DIV>
    <DIV class=""> This is another line of text</DIV>

Note that my test "foo" and "bar" text strings are inserted into the source
text so I know the "embeddeLinks" template is working, but the "a" element
and its attributes are completely missing. They must be getting lost in the
pass through the "paragraphs" template as it ignores everything that isn't a
string. I thought about just reversing the order in which the content goes
through the templates, but then I'd lose my source "link" element the same
way I'm losing my output "a" element.

I've considered trying to use "copy-of" in the "paragraphs" template, but I
can't quite see how to do it and still find the CR/LF codes. I'd appreciate
any input that could help me figure this one out.

Thanks,

-- Brook



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>
  • AW: Preserving inline elements when using string functions, Markus Abt <=