[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Wrapping consecutive similar elements inside a new

Subject: RE: Wrapping consecutive similar elements inside a new parent element
From: "Yates, Danny (ANTS)" <danny.yates@xxxxxxxxxx>
Date: Fri, 14 Feb 2003 12:27:30 -0000
ul inside p
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@xxxxxxx]
Sent: 14 February 2003 11:10
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.