Re: [xsl] Nodes have the same level

Subject: Re: [xsl] Nodes have the same level
From: "Joris Gillis" <roac@xxxxxxxxxx>
Date: Wed, 31 Aug 2005 18:42:41 +0200
Hi,
Tempore 08:44:39, die 08/31/2005 AD, hinc in xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit aaron apigo <aaronjose_apigo2001@xxxxxxxxx>:

It's very difficult for me to transform this XML
because the nodes have the same level (text:p), I
cannot used the <xsl:for-each> or maybe my stylesheet
was wrong.

By no means I try to disagree with Michael, but I cannot possibly resist to solve your problem as you posted it, nonetheless.


AFAIK, the input XML contains not enough information to reproduce the output you'd like to have. I attempted to create a generic solution, relying on the 'text:style-name' attributes to create hierarchy. However, after creating this solution I noticed that the styling information is too unreliable; it is not producing the hierachy.

Here's me attempt:

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

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="bookmark" match="*"
 use="preceding::text:bookmark-start[1]/@text:name"/>

<xsl:template match="/">
<root>
	<xsl:apply-templates select="//text:bookmark-start"/>
</root>
</xsl:template>

<xsl:template match="text:bookmark-start">
<xsl:element name="{following::text:user-field-get[1]/@text:name}">
	<xsl:attribute name="id">
		<xsl:value-of select="@text:name"/>
	</xsl:attribute>
	<ce:label>
		<xsl:value-of select="following::text:user-field-get[2]"/>
	</ce:label>
	<xsl:variable name="style-level"
	 select="following::text:p[1]/@text:style-name"/>
	<xsl:apply-templates select="key('bookmark',@text:name)[self::text:p]
	 [@text:style-name=$style-level]/text:user-field-get">
		<xsl:with-param name="style-hierarchy"
		select="concat($style-level,'-')"/>
	</xsl:apply-templates>
</xsl:element>
</xsl:template>

<xsl:template match="text:user-field-get">
<xsl:param name="style-hierarchy"/>
<xsl:element name="{@text:name}">
	<xsl:variable name="styler"
	 select="following::*[self::text:p or self::text:span]
	 [not(@text:style-name=substring-before($style-hierarchy,'-'))][1]"/>
	<xsl:variable name="style-level" select="$styler/@text:style-name"/>
	<xsl:variable name="my-id"
	 select="generate-id(ancestor::*[self::text:p or self::text:span][1])"/>
	<xsl:apply-templates select="(following::text:span[1]
	 [not(text:user-field-get)]|following-sibling::text())[1]"/>
	<xsl:if test="not(contains($style-hierarchy,$style-level))">
		<xsl:apply-templates
		 select="following::*[self::text:p or self::text:span]
		 [generate-id((preceding::*|ancestor::*)
		 [@text:style-name=substring-before($style-hierarchy,'-')]
		 [last()])=$my-id]
		 [@text:style-name=$style-level]/text:user-field-get">
			<xsl:with-param name="style-hierarchy"
			 select="concat($style-level,'-',$style-hierarchy)"/>
		</xsl:apply-templates>
	</xsl:if>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

---- gives output:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:ce="foobar/ce" xmlns:sb="foobar/sb" xmlns:text="foobar/text">
	<ce:bib-reference id="bib1">
		<ce:label>[1]</ce:label>
		<sb:reference>
			<sb:contribution>
				<sb:author>
					<ce:given-name>R.F.</ce:given-name>
					<ce:surname>Ziolo</ce:surname>
				</sb:author>
				<sb:author>
					<ce:given-name>E.P.</ce:given-name>
					<ce:surname>Giannelis</ce:surname>
				</sb:author>
			</sb:contribution>
			<sb:issue/>
			<sb:series>
				<sb:title>Science</sb:title>
				<sb:volume-nr>257</sb:volume-nr>
				<sb:date>1992</sb:date>
			</sb:series>
			<sb:pages>
				<sb:first-page>219</sb:first-page>
			</sb:pages>
		</sb:reference>
	</ce:bib-reference>
	<ce:bib-reference id="bib2">
		<ce:label>[2]</ce:label>
		<sb:reference>
			<sb:contribution>
				<sb:author>
					<ce:given-name>D.</ce:given-name>
					<ce:surname>Wirtz</ce:surname>
				</sb:author>
				<sb:author>
					<ce:given-name>M.</ce:given-name>
					<ce:surname>Fermigier</ce:surname>
				</sb:author>
			</sb:contribution>
			<sb:issue/>
			<sb:series>
				<sb:title>Phys. Rev. Lett</sb:title>
				<sb:volume-nr>72</sb:volume-nr>
				<sb:date>1994</sb:date>
			</sb:series>
			<sb:pages>
				<sb:first-page>2294</sb:first-page>
			</sb:pages>
		</sb:reference>
	</ce:bib-reference>
</root>


-- Joris Gillis (http://users.telenet.be/root-jg/me.html) B+Numquam mores quos extuli referoB; - Seneca , Ad Lucilium I, 7

P.S. My 2 copies of Mike's latest books have *finally* arrived; party at my place;)

Current Thread