xsl-list
[Top] [All Lists]

RE: [xsl] grabbing chunks of preceding-siblings between tables

2007-10-04 08:57:41
Instead of looking back, why not look forward? (XSLT gurus: from what I
understand, the following-sibling axis is much faster than the
preceding-sibling axis, right?)

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

  <xsl:key name  = "preceding-table-contents"
           match = "root/*[not(self::table)]"
           use   = "generate-id(following-sibling::table[1])"/>

  <xsl:template match="root">
    <xsl:copy>
      <!-- Single out the tables at this point;
           the <table>'s template will take care of the rest. -->
      <xsl:apply-templates select="@* | table"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="root/table">
    <xsl:copy>
      <!-- This will copy <table>'s attributes,
           the preceding nodes between this <table> and the last,
           and any child content of the <table>. -->
      <xsl:apply-templates select="@* | key('preceding-table-contents',
generate-id()) | node()"/>
    </xsl:copy>
  </xsl:template>

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

</xsl:stylesheet>


~ Scott


-----Original Message-----
From: Spencer Tickner [mailto:spencertickner(_at_)gmail(_dot_)com] 
Sent: Thursday, October 04, 2007 10:40 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] grabbing chunks of preceding-siblings between tables

Hi List,

Thanks in advance for the help. I'm doing some pre-processing of a XML
Dump of tables. I'm trying to associate header information in the
table to the table itself. I'm having difficulty figuring out how to
do this.. Anyway, examples always seem to say it better (XSLT 2.0 is
fine):

XML

<?xml version="1.0"?>
<root>
        <title1>Title for Table 1</title1>
        <title2>sub-title for Table 1</title2>
        <caption>Caption for Table 1</caption>
        <para_norm>Any number of different paragraphs that belong with
Table
1</para_norm>
        <table>
                <cell>Foo</cell>
                <cell>Bar</cell>
        </table>
        <title2>Table 2 only has subtitle</title2>
        <caption>Caption for Table 2</caption>
        <para_diff>Any number of different paragraphs that belong with
Table
2</para_diff>
        <table>
                <cell>Foo</cell>
                <cell>Bar</cell>
        </table>
        <title1>Title for Table 3</title1>
        <title2>sub-title for Table 3</title2>
        <caption>Caption for Table 3</caption>
        <para_norm>Any number of different paragraphs that belong with
Table
3</para_norm>
        <table>
                <cell>Foo</cell>
                <cell>Bar</cell>
        </table>
</root>

Desired XML Output:

<?xml version="1.0"?>
<root>
        <table>
                <title1>Title for Table 1</title1>
                <title2>sub-title for Table 1</title2>
                <caption>Caption for Table 1</caption>
                <para_norm>Any number of different paragraphs that
belong with Table
1</para_norm>
                <cell>Foo</cell>
                <cell>Bar</cell>
        </table>
        <table>
                <title2>Table 2 only has subtitle</title2>
                <caption>Caption for Table 2</caption>
                <para_diff>Any number of different paragraphs that
belong with Table
2</para_diff>
                <cell>Foo</cell>
                <cell>Bar</cell>
        </table>
        <table>
                <title1>Title for Table 3</title1>
                <title2>sub-title for Table 3</title2>
                <caption>Caption for Table 3</caption>
                <para_norm>Any number of different paragraphs that
belong with Table
3</para_norm>
                <cell>Foo</cell>
                <cell>Bar</cell>
        </table>
</root>

I've tried a number of different things, but can't seem to translate
"Take all elements between <table> and add as child to following
<table>" into XSLT. Any help would be greatly appreciated.

Thanks,

Spencer

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--