Re: Need help with this transformation

Martin Honnen <[email protected]>
Newsgroups gmane.comp.mozilla.devel.layout.xslt
Organization Liberty Development
Message-ID <[email protected]>
swegele wrote:

> *** I am still cuttin my teeth on thinking like XSL
> (declaritive/functional) So I could use some help figuring out the
> pattern to solve this problem of coallation and computation.
> To be honest if I solve the coallation problem I think I will be 90%
> there no?

The grouping can be solve like this

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

<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:key name="request-evidence" match="Result" 
use="concat(RequestNumber, '_', EvidenceNumber)"/>

<xsl:template match="Results">
   <xsl:copy>
     <xsl:apply-templates select="Result[generate-id() = 
generate-id(key('request-evidence', concat(RequestNumber, '_', 
EvidenceNumber))[1])]" mode="group"/>
   </xsl:copy>
</xsl:template>

<xsl:template match="Result" mode="group">
   <xsl:copy>
     <xsl:apply-templates select="RequestNumber | EvidenceNumber"/>
     <xsl:variable name="group" select="key('request-evidence', 
concat(RequestNumber, '_', EvidenceNumber))"/>
     <Result-List>
       <xsl:apply-templates select="$group/Result"/>
     </Result-List>
     <Deviation>
       <!-- compute deviation here -->
     </Deviation>
   </xsl:copy>
</xsl:template>

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

</xsl:stylesheet>

It gives the result you want beside my choice to not put an index into 
element names (e.g. Result1, Result2) which is bad XML design but use a 
Result-List parent element so you get e.g.

<Results>
   <Result>
     <RequestNumber>1</RequestNumber>
     <EvidenceNumber>1</EvidenceNumber>
     <Result-List>
       <Result>3.1</Result>
       <Result>3.2</Result>
     </Result-List>
     <Deviation/>
   </Result>
   <Result>
     <RequestNumber>2</RequestNumber>
     <EvidenceNumber>1</EvidenceNumber>
     <Result-List>
       <Result>3.1</Result>
       <Result>3.1</Result>
     </Result-List>
     <Deviation/>
   </Result>
</Results>

I am not sure how you want to compute the Deviation element content, in 
your example it just seems to be the difference of the two Result 
elements you have in each group but I am not sure your real XML input 
has only two Result elements for each group.

Note that general XSLT coding questions are better asked in a generic 
XML group (like comp.text.xml) or XSLT list, this group is mainly for 
specific problems with the XSLT implementation in Mozilla.


-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.