xsl-list
[Top] [All Lists]

Identity plus sort

2003-08-07 00:36:59
My basic xml looks like this, I dont know how many headers there will be or how many attributes on a matchList, matchItem, actionList or actionItem.

<?xml version="1.0" encoding="utf-8"?>
<doc>
 <header1>h1</header1>
 <header2>h2</header2>
 <header3>h3</header3>
 <matchList att1="val1">
   <matchItem date="2003-02-01">
     <actionList att1="val1">
       <actionItem>2002-02-01</actionItem>
       <actionItem>2002-03-01</actionItem>
       <actionItem>2002-01-01</actionItem>
     </actionList>
   </matchItem>
   <matchItem date="2003-01-01">
     <actionList att1="val2">
       <actionItem>2002-01-01</actionItem>
       <actionItem>2002-02-01</actionItem>
       <actionItem>2002-03-01</actionItem>
     </actionList>
   </matchItem>
   <matchItem date="2003-03-01">
     <actionList att1="val3">
       <actionItem>2002-03-01</actionItem>
       <actionItem>2002-01-01</actionItem>
       <actionItem>2002-02-01</actionItem>
     </actionList>
   </matchItem>
 </matchList>
</doc>

I'm trying to accomplish two things, firstly I need a copy of the document but with the match items sorted by the date attribute, within these matchItems I need the actionItems sorted by their content. My second requirement is to add some content at the bottom of the result tree, a further child of doc that has content based on the original document, I know how to create the content but not where to put it. If I place a newNode just after xsl:copy in the first template it gets added too many times.

Final result:

<?xml version="1.0" encoding="utf-8"?>
<doc>
 <header1>h1</header1>
 <header2>h2</header2>
 <header3>h3</header3>
 <matchList att1="val1">
   <matchItem date="2003-01-01">
     <actionList att1="val2">
       <actionItem>2002-01-01</actionItem>
       <actionItem>2002-02-01</actionItem>
       <actionItem>2002-03-01</actionItem>
     </actionList>
   </matchItem>
   <matchItem date="2003-02-01">
     <actionList att1="val1">
       <actionItem>2002-01-01</actionItem>
       <actionItem>2002-02-01</actionItem>
       <actionItem>2002-03-01</actionItem>
     </actionList>
   </matchItem>
   </matchItem>
   <matchItem date="2003-03-01">
     <actionList att1="val3">
       <actionItem>2002-01-01</actionItem>
       <actionItem>2002-02-01</actionItem>
       <actionItem>2002-03-01</actionItem>
     </actionList>
   </matchItem>
 </matchList>
<newNode><!-- content --></newNode>
</doc>

The xslt I tried looks like this:

<!-- The Identity Transformation -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <!-- Whenever you match any node or any attribute -->
 <xsl:template match="node()|@*">
   <!-- Copy the current node -->
   <xsl:copy>
     <!-- Including any child nodes it has and any attributes -->
     <xsl:apply-templates select="node()|@*"/>
   </xsl:copy>
 </xsl:template>
 <!--
   Templates to match nodes that need changing go here
 -->
 <xsl:template match="matchList">
   <xsl:copy>
     <xsl:for-each select="matchItem">
       <xsl:sort select="@date" order="ascending"/>
       <xsl:copy-of select="."/>
     </xsl:for-each>
   </xsl:copy>
 </xsl:template>

 <xsl:template match="actionList">
   <xsl:copy>
     <xsl:for-each select="actionItem">
       <xsl:sort select="." order="ascending"/>
       <xsl:copy-of select="."/>
     </xsl:for-each>
   </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

This has two failings, the last template is never called and the attributes are lost, presumably because I'm using xsl:copy.

Thanks in advance.

--

Joe

_________________________________________________________________
It's fast, it's easy and it's free. Get MSN Messenger today! http://www.msn.co.uk/messenger


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



<Prev in Thread] Current Thread [Next in Thread>