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 January 30th, 2008, 07:15 AM
Registered User
 
Join Date: Jan 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Extracting inline elements

Hello all, I am having a real problem getting my head round how to do a particular transform from one format to another. Basically I have an XML format to start with that contains <p> tags with inline <em> tags for header styles. I want to translate this XML to a format that has those elements as <h1>, <h2> etc at the same level as the <p>.

Its complicated so I'll give an example:

ORIGINAL XML:
<p>
<em style="H1">Heading text</em> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, <a href="examplelink">sed diam nonummy</a> nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. <em style="H2">Another heading text</em> Lorem ipsum dolor sit amet, <em style="bold">consectetuer</em> adipiscing elit.
</p>

DESIRED TRANSLATED XML:
<h1>Heading text</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, <a href="examplelink">sed diam nonummy</a> nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<h2>Another heading text</h2>
<p>Lorem ipsum dolor sit amet, <em style="bold">consectetuer</em> adipiscing elit.</p>


NOTE: The <p> tag is broken up in to a number of paragraphs and the <em style="bold"> and <a> tags need to remain inline.

I'm having trouble just working out the logic let alone actually coding it.

Any help would be appreciated!

Thanks
Rich








 
Old January 30th, 2008, 08:27 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Do you want to use XSLT 1.0 or XSLT 2.0?

 
Old January 30th, 2008, 08:28 AM
Registered User
 
Join Date: Jan 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oops sorry, I'm using XSLT 2.0

 
Old January 30th, 2008, 08:41 AM
Registered User
 
Join Date: Jan 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've been trying to use Recursion coupled with use of the Following Axis - my head feels like its going to explode!

I'm really stumped :(


 
Old January 30th, 2008, 09:05 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I think you can use xsl:for-each-group group-adjacent.
Here is an example stylesheet that works for me with Saxon 9:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:template match="root">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="p">
    <xsl:for-each-group select="node()" 
                        group-adjacent="self::em[@style = 'H1'] or self::em[@style = 'H2']">
      <xsl:choose>
        <xsl:when test="current-grouping-key()">
          <xsl:apply-templates select="current-group()"/>
        </xsl:when>
        <xsl:otherwise>
          <p>
            <xsl:apply-templates select="current-group()"/>
          </p>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:template>

  <xsl:template match="em[@style = 'H1']">
    <h1>
      <xsl:apply-templates/>
    </h1>
  </xsl:template>

  <xsl:template match="em[@style = 'H2']">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>

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

</xsl:stylesheet>


 
Old January 30th, 2008, 09:18 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Note that with my suggested stylesheet and your original input you will get a leading
Code:
<p>
</p>
because of the whitespace before the first child element.
You can avoid that by using
Code:
    <xsl:for-each-group select="node()[not(self::text()[not(normalize-space())])]" 
                        group-adjacent="self::em[@style = 'H1'] or self::em[@style = 'H2']">
You might also want <xsl:output indent="yes"/>

 
Old January 30th, 2008, 09:59 AM
Registered User
 
Join Date: Jan 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thats great, I will give that a try within the transform I am using.

Thanks so much for your help!

Rich






Similar Threads
Thread Thread Starter Forum Replies Last Post
inline PDF naheedv Pro JSP 3 June 8th, 2007 01:34 AM
Inline Javascript. rupen Javascript How-To 1 July 20th, 2005 12:27 PM
How inline view works ? jap SQL Server 2000 0 June 6th, 2005 03:34 AM
Inline Code to Project rodmcleay VS.NET 2002/2003 1 December 14th, 2004 10:38 PM
error in inline code yoord BOOK: Beginning ASP.NET 1.0 1 November 1st, 2004 05:13 AM





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