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

Subject: RE: [xsl] grabbing chunks of preceding-siblings between tables
From: "Scott Trenda" <Scott.Trenda@xxxxxxxx>
Date: Thu, 4 Oct 2007 11:00:06 -0500
Argh, I had told myself I wouldn't need it, then I put it in anyway. You
don't need the [1] in the <xsl:key/> statement, as generate-id() will
only take the first one anyway. The stylesheet quoted below has been
edited to reflect this.

~ Scott


-----Original Message-----
From: Scott Trenda [mailto:Scott.Trenda@xxxxxxxx]
Sent: Thursday, October 04, 2007 10:57 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] grabbing chunks of preceding-siblings between tables

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)"/>

  <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@xxxxxxxxx]
Sent: Thursday, October 04, 2007 10:40 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
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

Current Thread