RE: [xsl] empty element sabotages xsl:key revisited

Subject: RE: [xsl] empty element sabotages xsl:key revisited
From: "McNally, David" <David.McNally@xxxxxxxxxx>
Date: Tue, 11 Feb 2003 12:38:11 -0500
If the use attribute of xsl:key returns a node-set, the key is set to the
string values of one or more of the nodes in the node-set.  So by saying:

<xsl:key name="structures" match="Structure" use="preceding-sibling::*[1]"/>

you are actually setting the key to the text of preceding-sibling::*[1],
which is not what you want, and explains the empty element problem.  You
would see a similar problem if you had multiple elements with the same
non-null text content.

Maybe you want to use:

<xsl:key name="structures" match="Structure"
use="generate-id(preceding-sibling::*[1])"/>

and 

<xsl:copy-of select="key('structures', generate-id(.))"/>

David.
--
David McNally            Moody's Investors Service
Software Engineer        99 Church St, NY NY 10007 
David.McNally@xxxxxxxxxx            (212) 553-7475 

> -----Original Message-----
> From: Antonie Botes [mailto:antonie.botes@xxxxxxxxxxx]
> Sent: Tuesday, February 11, 2003 12:01 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] empty element sabotages xsl:key revisited
> 
> 
> From a post in the archive
> (http://www.biglist.com/lists/xsl-list/archives/200102/msg0070
> 6.html) it
> seems that a similar problem was encountered, but solved by using a
> different parser
> (http://www.biglist.com/lists/xsl-list/archives/200102/msg0072
> 8.html). 
> 
> I have tried the following using Xalan 2.4.1, and MSXSL 4.0, 
> with the same
> results:
> 
> I use keys to identify a <Structure> by using its first 
> preceding sibling as
> its lookup value. The effect is that when an element that is 
> followed by a
> <Structure> in the source, is added to the result tree, the 
> structure is
> also added (see <xsl:template match="*">). The output is 
> wrong however, and
> some of the structures are added following some of the empty 
> elements ( e.g.
> <EnvProfileText/> ). 
> 
> After removing these specific empty elements, the wrong 
> output disappears. I
> don't have a clue why it behaves as it does, as the 'offending' empty
> elements has no relation to the key as I see it. Is the 
> problem with the
> processors, or with my 16:00-in-the-afternoon logic?
> 
> PS - This is the second part of a 2 phase XSLT conversion. 
> The first part is
> used to simplify an existing source of XML, the second part is to add
> structure/grouping to the result. 
> 
> PPS - If I missed an easier/more practical way to do this, 
> please feel free
> to let me know!
> 
> Source XML:
> -----------
> <?xml version="1.0" encoding="UTF-16"?>
> <APMainProfile  xmlns:xlink="http://www.w3.org/1999/xlink";>
> 	<StructureHeading>STRUCTURE: </StructureHeading>
> 	<Structure  xlink:href="#./Obj0013D1A7" xlink:type="simple"
> xlink:show="embed" xlink:actuate="onLoad"/>
> 	<EnvProfileText>Pyriftalid is harmless to earthworms, bees and
> birds. The product also exhibits low toxicity to fish, 
> daphnia, algae and is
> not expected to cause risk to aquatic organisms due to its 
> low use rates,
> fast degradation in the paddy, rapid adsorbtion to sediments, 
> low aqueous
> solubility and low logP.</EnvProfileText>
> 
> <!-- Problematic empty element -->
> 	<EnvProfileText/>
> 	<EnvProfileGeneral>Fate in soil: </EnvProfileGeneral>
> 	<SynthesisRouteHeading>SYNTHESIS ROUTES:</SynthesisRouteHeading>
> 	<SynthesisRouteDescription>
> 		<a xlink:type="simple" 
> xlink:href="../company/syngentaw.htm"
> ProfileType="company">Syngenta</a> has extensively studied 
> the synthesis
> routes to pyriftalid and the following conclusions can be
> made;</SynthesisRouteDescription>
> 	<SynthesisRouteHeading/>
> 	<Structure xlink:href="#./Obj0013D1A8" xlink:type="simple"
> xlink:show="embed" xlink:actuate="onLoad"/>
> 	<Structure xlink:href="#./Obj0013D1A9" xlink:type="simple"
> xlink:show="embed" xlink:actuate="onLoad" />
> 	<Structure xlink:href="#./Obj0013D1AA" xlink:type="simple"
> xlink:show="embed" xlink:actuate="onLoad" />
> </APMainProfile>
> 
> XSLT Code:
> ----------
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> 	<xsl:output indent="yes" method="xml"/>
> 	<xsl:key name="structures" match="Structure"
> use="preceding-sibling::*[1]"/>
> 	<xsl:template match="/APMainProfile">
> 		<xsl:processing-instruction
> name="xml-stylesheet">type="text/css"
> href="APMain.css"</xsl:processing-instruction>
> 		<xsl:copy>
> 			<StructureSection>
> 				<xsl:apply-templates
> select="StructureHeading | StructureDescription"/>
> 			</StructureSection>
> 			<EnvProfileSection>
> 				<xsl:apply-templates
> select="EnvProfileHeading | EnvProfileCreature | EnvProfileValue |
> EnvProfileClass | EnvProfileGeneral | EnvProfileText"/>
> 			</EnvProfileSection>
> 			<SynthesisRouteSection>
> 				<xsl:apply-templates
> select="SynthesisRouteHeading | SynthesisRouteDescription |
> SynthesisRouteDiagram"/>
> 				<SynthesisRouteDiagram>
> 				</SynthesisRouteDiagram>
> 			</SynthesisRouteSection>
> 		</xsl:copy>
> 	</xsl:template>
> 	<xsl:template match="*">
> 		<xsl:copy-of select="."/>
> 		<xsl:copy-of select="key('structures', .)"/>
> 	</xsl:template>
> </xsl:stylesheet>
> 
> Result:
> -------
> <?xml version="1.0" encoding="UTF-8"?>
> <?xml-stylesheet type="text/css" href="APMain.css"?>
> <APMainProfile xmlns:xlink="http://www.w3.org/1999/xlink";>
> 	<StructureSection>
> 		<StructureHeading>STRUCTURE: </StructureHeading>
> 		<Structure xlink:href="#./Obj0013D1A7" 
> xlink:type="simple"
> xlink:show="embed" xlink:actuate="onLoad"/>
> 	</StructureSection>
> 	<EnvProfileSection>
> 		<EnvProfileText>Pyriftalid is harmless to 
> earthworms, bees
> and birds. The product also exhibits low toxicity to fish, 
> daphnia, algae
> and is not expected to cause risk to aquatic organisms due to 
> its low use
> rates, fast degradation in the paddy, rapid adsorbtion to 
> sediments, low
> aqueous solubility and low logP.</EnvProfileText>
> 		<EnvProfileText/>
> 
> <!-- Problematic output starts here -->
> 		<Structure xlink:href="#./Obj0013D1A8" 
> xlink:type="simple"
> xlink:show="embed" xlink:actuate="onLoad"/>
> 		<Structure xlink:href="#./Obj0013D1A9" 
> xlink:type="simple"
> xlink:show="embed" xlink:actuate="onLoad"/>
> 		<Structure xlink:href="#./Obj0013D1AA" 
> xlink:type="simple"
> xlink:show="embed" xlink:actuate="onLoad"/>
> <!-- Problematic output ends here -->
> 
> 		<EnvProfileGeneral>Fate in soil: </EnvProfileGeneral>
> 	</EnvProfileSection>
> 	<SynthesisRouteSection>
> 		<SynthesisRouteHeading>SYNTHESIS
> ROUTES:</SynthesisRouteHeading>
> 		<SynthesisRouteDescription>
> 			<a xlink:type="simple"
> xlink:href="../company/syngentaw.htm" 
> ProfileType="company">Syngenta</a> has
> extensively studied the synthesis routes to pyriftalid and 
> the following
> conclusions can be made;</SynthesisRouteDescription>
> 		<SynthesisRouteHeading/>
> 		<Structure xlink:href="#./Obj0013D1A8" 
> xlink:type="simple"
> xlink:show="embed" xlink:actuate="onLoad"/>
> 		<Structure xlink:href="#./Obj0013D1A9" 
> xlink:type="simple"
> xlink:show="embed" xlink:actuate="onLoad"/>
> 		<Structure xlink:href="#./Obj0013D1AA" 
> xlink:type="simple"
> xlink:show="embed" xlink:actuate="onLoad"/>
> 		<SynthesisRouteDiagram/>
> 	</SynthesisRouteSection>
> </APMainProfile>
> 
> This email is confidential and is not intended to be read by anyone
> other than the named recipient. If you have received this 
> email in error,
> please notify us immediately by reply email and then delete 
> this message from
> your system: please do not copy this email or use it for any 
> purpose or
> disclose its contents to any other person (since this could 
> be a breach
> of confidence) and any hard copy should be destroyed immediately.
> Thank you for your co-operation.
> 
> WARNING: please check any attachment for viruses before opening it as
> PJB Publications Ltd accepts no liability for this email. 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


---------------------------------------

The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission.  If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited.  If you have received this message in error, please immediately notify us by telephone, fax or e-mail and delete the message and all of its attachments.  Thank you.

Every effort is made to keep our network free from viruses.  You should, however, review this e-mail message, as well as any attachment thereto, for viruses.  We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message.


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


Current Thread