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 July 23rd, 2016, 08:26 AM
Registered User
 
Join Date: Jul 2016
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default fix broken tagging

I have some broken tags in my input that I like to fix in my xslt.
I have searched and tried different things, but I can't seem to figure it out.

Input:

Code:
<p>Some text <span class="italic">broken </span><span class="italic">tag</span><span class="italic">s</span> in my content which is <span class="italic">really </span><span class="italic">annoying</span>.</p>
Desired output:

Code:
<p>Some text <span class="italic">broken tags</span> in my content which is <span class="italic">really annoying</span>.</p>
Can someone help me in the right direction?
 
Old July 23rd, 2016, 08:55 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Can you use an XSLT 2.0 processor like Saxon 9? Then it is a task for
Code:
for-each-group group-adjacent
. You will have to explain in more detail what kind of elements you want to merge ( only "span" elements?) to get help with a code sample.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old July 23rd, 2016, 09:02 AM
Registered User
 
Join Date: Jul 2016
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Martin,

I use xslt 2.0 and indeed I would only like to merge the span\@italic

Regards
Heinrich
 
Old July 23rd, 2016, 09:13 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you have access to commercial editions of Saxon 9.6 or 9.7 (for instance with oXygen or Stylus Studio) then you can do it in a generic way with XSLT 3.0 and a composite grouping key in group-ajacent:

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="3.0">
    
    <xsl:mode on-no-match="shallow-copy"/>
    
    <xsl:template match="p">
        <xsl:copy>
            <xsl:for-each-group select="node()" group-adjacent="node-name(.), @class" composite="true">
                <xsl:copy>
                    <xsl:copy-of select="@class"/>
                    <xsl:apply-templates select="current-group()/node()"/>
                </xsl:copy>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>
With XSLT 2.0 you could use a similar approach which concatenates the node-name() and the class attribute for the group-adjacent value, or as you have now posted some details you could use

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="2.0">
  
    <xsl:template match="@* | node()">
       <xsl:copy>
         <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
   </xsl:template>

    <xsl:template match="p">
        <xsl:copy>
            <xsl:for-each-group select="node()" group-adjacent="self::span and @class = 'italic'">
              <xsl:choose>
                <xsl:when test="current-grouping-key()">
                <xsl:copy>
                    <xsl:copy-of select="@class"/>
                    <xsl:apply-templates select="current-group()/node()"/>
                </xsl:copy>
               </xsl:when>
               <xsl:otherwise>
                  <xsl:apply-templates select="current-group()"/>
               </xsl:otherwise>
              </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog

Last edited by Martin Honnen; July 23rd, 2016 at 09:19 AM.. Reason: trying to address comment
 
Old July 24th, 2016, 03:49 AM
Registered User
 
Join Date: Jul 2016
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Martin!
This works fine!





Similar Threads
Thread Thread Starter Forum Replies Last Post
<struct><cell> tagging list-elements Jenny91 XSLT 2 March 26th, 2014 05:19 PM
Tagging vantoko BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 July 5th, 2007 01:19 AM
SignOut is broken? cohansh1 BOOK: ASP.NET Website Programming Problem-Design-Solution 1 June 21st, 2005 01:09 AM
broken cd waz84792 Flash (all versions) 5 October 22nd, 2004 08:42 AM





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