Re: [xsl] pulling child elements up to sibling level

Subject: Re: [xsl] pulling child elements up to sibling level
From: "Rick Geimer" <Rick.Geimer@xxxxxxx>
Date: Tue, 27 May 2003 09:26:36 -0700
Thanks, with a little modification this did the trick. Here is the final
stylesheet in case anyone is interested:

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

<xsl:key name="pnodes" match="para/node()" use="generate-id((..|
(preceding-sibling::*|.)[self::unordered.list or self::ordered.list])[last
()])"/>

<xsl:template match="para">
<xsl:variable name="p1">
<para>
 <xsl:apply-templates select="key('pnodes',generate-id(.))"/>
</para>
</xsl:variable>
<xsl:if test="key('pnodes',generate-id(.))/self::* or normalize-space
($p1)">
  <xsl:copy-of select="$p1"/>
</xsl:if>
<xsl:for-each select="unordered.list|ordered.list">
<xsl:apply-templates select="."/>
<xsl:variable name="p" select="key('pnodes',generate-id(.))[position() &gt;
1]"/>
 <xsl:if test="$p[self::* or normalize-space(.)]">
<para>
<xsl:apply-templates select="$p"/>
</para>
</xsl:if>
</xsl:for-each>
</xsl:template>


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

</xsl:stylesheet>

<!-- SOURCE XML-->

<?xml version="1.0"?>
<doc>
<para>Here is a <emphasis>paragraph</emphasis>with
      <unordered.list>
            <list.item>unordered</list.item>
            <list.item>lists</list.item>
      </unordered.list>
and
      <ordered.list>
            <list.item>ordered</list.item>
            <list.item>lists</list.item>
      </ordered.list>
embedded inside of it along with some <emphasis>formatting</emphasis>
tags</para>
</doc>

Rick



Date: Fri, 23 May 2003 17:51:29 +0100
From: David Carlisle <davidc@xxxxxxxxx>
Subject: Re: [xsl] pulling child elements up to sibling level

  My target schema only allows lists and such as siblings of paragraphs, thus
  I need to transform the document into something like this:



This is what I use in the stylesheet for the mathml spec to pull inline
lists tables and stuff out for the utterly useless content model of an
html p.  Adjust the list of elements to taste.



<!-- NESTED PARAGRAPHAS -->

<!-- xmlspec, like most document formats other than html, allows
    block level elements in paragraphs. For hTML need to
    separately wrap each conecutive run of inline elements in <p>
    and leave block level elements outside the p.

   xmlspec.xsl has some code using disable-output-escaping to
   attempt to do this, but it doesn't really work, even with processors
   that support d-o-e. The original mathml stylesheet as used for
   mathml 1.01 had code to deal with this, but this is a more
   efficient implementation using the grouping technique using keys.
- -->


<xsl:key name="pnodes" match="p/node()"
                  use="generate-id((..|
                     (preceding-sibling::*|.)[
          self::eg
       or self::glist
       or self::olist
       or self::ulist
       or self::slist
       or self::orglist
       or self::table
       or self::issue
       or self::note
       or self::processing-instruction()
       or self::graphic[not(@role='inline')]
        ])[last()])"/>

<xsl:template match="p">
<xsl:variable name="p1">
<p>
        <xsl:if test="@id">
         <xsl:attribute name="id"><xsl:value-of
  select="translate(@id,'_','.')"/></xsl:attribute>
        </xsl:if>
 <xsl:if test="@role">
   <xsl:attribute name="class">
      <xsl:value-of select="@role"/>
   </xsl:attribute>
 </xsl:if>
 <xsl:apply-templates select="key('pnodes',generate-id(.))"/>
</p>
</xsl:variable>
<xsl:if test="key('pnodes',generate-id(.))/self::* or normalize-space($p1)">
  <xsl:copy-of select="$p1"/>
</xsl:if>
<xsl:for-each select="eg|glist|olist|ulist|slist|orglist|
                       table|issue|note|processing-instruction()|
                       graphic[not(@role='inline')]">
<xsl:apply-templates select="."/>
<xsl:variable name="p" select="key('pnodes',generate-id(.))[position() &gt; 1]"/>
 <xsl:if test="$p[self::* or normalize-space(.)]">
<p>
<xsl:apply-templates select="$p"/>
</p>
</xsl:if>
</xsl:for-each>
</xsl:template>


<!-- END OF NESTED PARAGRAPHAS -->









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


Current Thread