Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 19th, 2011, 10:40 AM
Registered User
 
Join Date: Sep 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy looping through siblings to change br tags to p tags

Hi, I have this input xml

<body>

<p class="heading">
<span><tag>This text</tag>
<br/>
</span>
</p>

<p class="bodyText">
<span><tag>is</tag>
<br/>
</span>
<span><tag>meant</tag>
</span>
<span><tag>to</tag>
<br/>
</span>
</p>

<p class="bodyText">
<span>
<tag>be</tag></span>
<span>
<tag>read</tag>
<br/>
</span>
<span><tag>with</tag></span>
<span><tag>some</tag></span>
<span><tag>inline</tag><br/></span>
<span class="italic">
<tag>styles</tag>
<br/>
<tag>the</tag>
<br/>
<tag>end</tag>
</span>
</p>

</body>


I wish to get this output - replacing the br tags with p tags and inheriting any classes


<body>

<p class="heading">
<span>
<tag>This text</tag>
</span>
</p>

<p class="bodyText">
<span>
<tag>is</tag>
</span>
</p>

<p class="bodyText">
<span>
<tag>meant</tag>
</span>
<span>
<tag>to</tag>
</span>
</p>

<p class="bodyText">
<span>
<tag>be</tag>
</span>
<span>
<tag>read</tag>
</span>
</p>

<p class="bodyText">
<span>
<tag>with</tag>
</span>
<span>
<tag>some</tag>
</span>
<span>
<tag>inline</tag>
</span>
</p>

<p class="bodyText">
<span class="italic">
<tag>styles</tag>
</span>
</p>

<p class="bodyText">
<span class="italic">
<tag>the</tag>
</span>
</p>

<p class="bodyText">
<span class="italic">
<tag>end</tag>
</span>
</p>

</body>


XSLT: So far I have;


<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="p">
<xsl:apply-templates select="span[1]" mode="group"/>
</xsl:template>

<xsl:template match="span[not(br)]" mode="group">
<p>
<xsl:apply-templates select="."/>
</p>
<xsl:apply-templates select="following-sibling::span[br][1]/following-sibling::span[1]" mode="group"/>
</xsl:template>

<xsl:template match="span[br]" mode="group">
<p>
<xsl:apply-templates select="."/>
</p>
<xsl:apply-templates select="following-sibling::span[1]" mode="group"/>
</xsl:template>


<xsl:template match="span[not(br)]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::span[1]"/>
</xsl:template>

<xsl:template match="span/br"/>

</xsl:stylesheet>



Which works up and until the last set of br's in the last span tag of the input. I also have this XSLT which does the same but using a key;


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>

<xsl:key name="para" match="span" use="generate-id(following-sibling::br[1])" />

<xsl:template match="/">
<body>
<xsl:apply-templates select="body/p" />
</body>
</xsl:template>

<xsl:template match="p">
<xsl:apply-templates select="br" />
<xsl:if test="span[not(following-sibling::br)]">
<p>
<xsl:apply-templates select="span[not(following-sibling::br)]" />
</p>
</xsl:if>
</xsl:template>

<xsl:template match="br">
<p>
<xsl:apply-templates select="key('para', generate-id())" />
</p>
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

...and still the same issue. I would be very gratful if some one could point out what I need to do with either piece of XSLT.

many thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL TAGS HIDING eg <BR> simondev Crystal Reports 2 March 8th, 2014 03:33 AM
Looping through a list using logic tags problem aadz5 Struts 3 September 23rd, 2011 03:18 AM
how can split/replace the <br> with <Para> tags. moshaik XML 2 March 8th, 2009 11:11 AM
Access/change html-tags contained in elementvalues polarbear XSLT 3 December 26th, 2007 07:23 AM
How to change your own tags in the forum? KingCool BOOK: ASP.NET Website Programming Problem-Design-Solution 3 April 17th, 2007 01:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.