[xsl] Many sets of eyes ...

Subject: [xsl] Many sets of eyes ...
From: Curtis Burisch <burisch@xxxxxxxxxxx>
Date: Mon, 11 Feb 2002 15:49:56 +0000
Hi,

A few days ago I had a problem to solve and was tempted to take it to 
the list but decided to grit my teeth and just get on with it. I'm 
still pretty new to XSLT so I'm afraid I've probably made some gaffes 
in my solution. I was hoping that the trained eyes on the list (e.g. 
the Uber-Deities Jeni, Michael, Jim, et. al) can give me some pointers 
on stylistic / performance issues? And of course help me point out 
gaffes ;-)

Given the xml source:

<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<wrap>
<joe>Apples</joe>
</wrap>
<wrap>
<joe>Bananas</joe>
</wrap>
<wrap>
<ann>Pears</ann>
</wrap>
<wrap>
<joe>Oranges</joe>
</wrap>
</root>

And the desired output:

Joe says: "Apples, Bananas."
Ann says: "Pears."
Joe says: "Oranges."

My implementation is:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:output method="text" encoding="ISO-8859-1" indent="no"/>
<xsl:strip-space elements="*"/>

<xsl:template match="root">
<xsl:for-each select="wrap">

<xsl:if test="name(preceding-sibling::*[position()=1]/*) != name(./*)">

<xsl:if test="name(./*[1])='joe'">
<xsl:text>Joe says: "</xsl:text>
</xsl:if>
<xsl:if test="name(./*[1])='ann'">
<xsl:text>Ann says: "</xsl:text>
</xsl:if>
</xsl:if>

<xsl:if test="joe">
<xsl:apply-templates select="joe"/>
</xsl:if>
<xsl:if test="ann">
<xsl:apply-templates select="ann"/>
</xsl:if>

<xsl:if test="name(following-sibling::*[position()=1]/*) = name(./*)">
<xsl:text>, </xsl:text>
</xsl:if>

<xsl:if test="name(following-sibling::*[position()=1]/*) != name(./*)">

<xsl:text>."&#x000A;</xsl:text>
</xsl:if>

</xsl:for-each>
</xsl:template>

<xsl:template match="joe">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="ann">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>


I chose to put Joe and Ann into templates because the actual 
application I am writing requires more complex processing for each 
different node type. But this example matches the real situation fairly 
closely ...

Thanks,
Curtis.

-- 

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


Current Thread