This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: HTML in an XML tag


[Mac Rost]

[[
I would like to find a solution to this.... in the following XML tag,
there is a standard HTML <br> within the paragraph. I was wondering if
there was a way to incorporate the tag into the transformation.
-----------------
<policy>HHonors points or related frequent traveler miles are not
honored on guest rooms reserved via Hotels.com. &amp;lt;br&amp;gt; You
must present a photo ID when checking in.
]]

Notice that there is not a "standard <br>" in there.  Rather, there is text
that, when displayed on a browser, **looks like** a <br>.  I assume that you
would like an actual <br> element to be inserted.  You can do this provided
that there is not going to be a variable amount of whitespace between the
"b" and the (escaped) angle brackets.

The method is actually the same one to use for your second question, about
separating by "Bed Type:", so I will just give the approach to the first,
and you will be able to make it work for the second (I am assuming that you
mean to separate the sections by a <br/> element and not contain them in
separate elements, which would be harder to do).

The problem here is to detect where a particular string occurs, and wherever
it does, replace it with something else.  You do not know the exact number
of these marker strings that may be present

You can do this by means of a recursive template, one that calls itself.
You split the string at the separator, which in this case is
"&amp;lt;br&amp;gt;", using substring-before() and substring-after().  In
between the two fragments you put your <br/> or whatever you want to go
there.  The rest of the string, the substring-after() part, you feed back to
the template to repeat until there are no more separators present.

The slightly tricky part is that, if no separator is present,
substring-before() returns not the whole string but nothing, so you have to
test for that case and handle it differently.

Here is the xslt to do this job.  I have wrapped the result in a "policy"
element but of course you can use any element you want.  Obviously you could
add one more parameter to insert anything, not just a <br/> element.

===============================================
<xsl:template match='policy'>
<policy>
<!-- Call the recursive string splitter -->
 <xsl:call-template name='break-at-string'>
    <xsl:with-param name='data' select='.'/>
    <xsl:with-param name='separator' select='"&amp;lt;br&amp;gt;"'/>
 </xsl:call-template>
</policy>
</xsl:template>

<xsl:template name='break-at-string'>
   <xsl:param name='data'/>
   <xsl:param name='separator'/>

  <xsl:variable name='first' select='substring-before($data,$separator)'/>
  <xsl:variable name='rest' select='substring-after($data,$separator)'/>

 <xsl:choose>
     <!-- When there is another separator in the string, display
            the first part then call ourself to process the rest  -->
    <xsl:when test='$rest'>
       <xsl:value-of select='$first'/><br/>
       <xsl:call-template name='break-at-string'>
            <xsl:with-param name='data' select='$rest'/>
            <xsl:with-param name='separator' select='$separator'/>
       </xsl:call-template>
   </xsl:when>

   <!-- If there are no more separators, we are done -->
   <xsl:otherwise>
       <xsl:value-of select='$data'/>
   </xsl:otherwise>
 </xsl:choose>
</xsl:template>

===============================================

Cheers,

Tom P


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]