xsl-list
[Top] [All Lists]

RE: Wrapping consecutive similar elements inside a new pare nt element

2003-02-14 05:27:30
How about this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xml"/>
  
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="doc">
    <doc>
      <xsl:apply-templates select="p[not(span[substring(text(),1,1) =
'-'])]"/>
    </doc>
  </xsl:template>
  
  <xsl:template match="p[not(span[substring(text(),1,1) = '-'])]">
    <xsl:copy-of select="."/>
    <xsl:if test="following-sibling::p[span[substring(text(),1,1) = '-']]">
      <ul>
        <xsl:for-each
select="following-sibling::p[span[substring(text(),1,1) = '-']][
                count(preceding-sibling::p[not(span[substring(text(),1,1) =
'-'])][1] | current()) = 1]">
          <xsl:apply-templates select="."/>
        </xsl:for-each>
      </ul>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="p[span[substring(text(),1,1) = '-']]">
    <li><xsl:value-of select="text()"/></li>
  </xsl:template>
</xsl:stylesheet>

-----Original Message-----
From: Graham Hannington [mailto:Ghannington(_at_)csl(_dot_)com]
Sent: 14 February 2003 11:10
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Wrapping consecutive similar elements inside a new parent
element


I'm cleaning up HTML exported from Word documents.  First, I run Tidy again
the exported HTML, to convert it to well-formed XHTML, then I transform it
with my own XSLT stylesheet.

I need to convert (this is slightly simplified):


<p>Some paragraph that does not begin with a span element whose first
character is a hyphen.</p>
<p><span>-</span>List item</p>
<p><span>-</span>List item</p>
<p><span>-</span>List item</p>
<p><span>-</span>List item</p>
<p>Some paragraph that does not begin with a span element whose first
character is a hyphen.</p>
<p><span>-</span>List item</p>
<p><span>-</span>List item</p>
<p><span>-</span>List item</p>
<p><span>-</span>List item</p>
<p>Some paragraph that does not begin with a span element whose first
character is a hyphen.</p>


into this:


<p>Some paragraph that does not begin with a span element whose first
character is a hyphen.</p>
<ul>
<li>List item</li>
<li>List item</li>
<li>List item</li>
<li>List item</li>
</ul>
<p>Some paragraph that does not begin with a span element whose first
character is a hyphen.</p>
<ul>
<li>List item</li>
<li>List item</li>
<li>List item</li>
<li>List item</li>
</ul>
<p>Some paragraph that does not begin with a span element whose first
character is a hyphen.</p>


I'm afraid my attempts at this have been truly pitiful:


        <xsl:template match="p[starts-with(span[1], '-')]">

                <xsl:if
test="not(starts-with(preceding-sibling::p[1]/span[1], '-'))">
                        <xsl:text>&lt;ul&gt;</xsl:text>
                </xsl:if>

                <li>
                        <xsl:apply-templates />
                </li>

                <xsl:if
test="not(starts-with(following-sibling::p[1]/span[1], '-'))">
                        <xsl:text>&lt;/ul&gt;</xsl:text>
                </xsl:if>
        </xsl:template>


I know, I know: those <xsl:text>&lt;ul&gt;</xsl:text> bits are a joke, they
don't produce the <ul> parent element.

I've tried writing a template that uses <xsl:for-each>, but that ends up
grabbing all following-sibling <p> elements that begin with <span>-</span>.
I don't know how to stop the <xsl:for-each> at the first non-matching <p>
element.

Can anyone throw me an XSLT pearl?

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


***************************************************************************
This communication (including any attachments) contains confidential 
information.  If you are not the intended recipient and you have received this 
communication in error, you should destroy it without copying, disclosing or 
otherwise using its contents.  Please notify the sender immediately of the 
error.

Internet communications are not necessarily secure and may be intercepted or 
changed after they are sent.  Abbey National Treasury Services plc does not 
accept liability for any loss you may suffer as a result of interception or any 
liability for such changes.  If you wish to confirm the origin or content of 
this communication, please contact the sender by using an alternative means of 
communication.

This communication does not create or modify any contract and, unless otherwise 
stated, is not intended to be contractually binding.

Abbey National Treasury Services plc. Registered Office:  Abbey National House, 
2 Triton Square, Regents Place, London NW1 3AN.  Registered in England under 
Company Registration Number: 2338548.  Regulated by the Financial Services 
Authority (FSA).
***************************************************************************


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



<Prev in Thread] Current Thread [Next in Thread>