Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Sort Issue After spawning new nodes

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 9/10/2008 4:53:00 PM

Bruce wrote:

> Now of course, real users want this sorted.  I find that I'm running into a 
> block here.  Any chunk of rows that were originally brokent out of the string 
> want to always sort together.  I assume xsl:sort is looking past the artifice 
> of my fancy footwork and just sorting them where they REALLY are, since the 
> result of all this is really just pointers pointing to pointers.  How can I 
> fool xsl:sort TOO so that it allows me to sort Zebra to the end of the list?

Sorry, from the information you have provided it is not possible to see 
where things go wrong, we would need to see your stylesheet. It should 
be possible to create a result tree fragment with new Row elements, then 
convert that to a node-set and then to sort that node-set as needed.

For instance with the XML input being

<root>
   <Row item1="Label" item2="aardvark"/>
   <Row item1="Label" item2="echidna;zebra;bear;orangutang"/>
   <Row item1="Label" item2="kangaroo"/>
</root>

and this stylesheet

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xmlns:exsl="http://exslt.org/common"
                 exclude-result-prefixes="exsl">
   <xsl:output method="xml" indent="yes"/>
   <xsl:template match="root">
     <xsl:copy>
       <xsl:variable name="row-rtf">
         <xsl:apply-templates select="Row"/>
       </xsl:variable>
       <xsl:apply-templates select="exsl:node-set($row-rtf)/Row" 
mode="copy">
         <xsl:sort select="@item2"/>
       </xsl:apply-templates>
     </xsl:copy>
   </xsl:template>
   <xsl:template match="Row" mode="copy">
     <xsl:copy-of select="."/>
   </xsl:template>
   <xsl:template match="Row[contains(@item2, ';')]">
     <xsl:call-template name="make-rows">
       <xsl:with-param name="row" select="."/>
       <xsl:with-param name="items" select="@item2"/>
     </xsl:call-template>
   </xsl:template>
   <xsl:template match="Row[not(contains(@item2, ';'))]">
     <xsl:copy-of select="."/>
   </xsl:template>
   <xsl:template name="make-rows">
     <xsl:param name="row"/>
     <xsl:param name="items"/>
     <xsl:choose>
       <xsl:when test="contains($items, ';')">
         <xsl:element name="{name($row)}">
           <xsl:copy-of select="@*[not(name() = 'item2')]"/>
           <xsl:attribute name="item2">
             <xsl:value-of select="substring-before($items, ';')"/>
           </xsl:attribute>
         </xsl:element>
         <xsl:call-template name="make-rows">
           <xsl:with-param name="row" select="$row"/>
           <xsl:with-param name="items" select="substring-after($items, 
';')"/>
         </xsl:call-template>
       </xsl:when>
       <xsl:otherwise>
         <xsl:element name="{name($row)}">
           <xsl:copy-of select="@*[not(name() = 'item2')]"/>
           <xsl:attribute name="item2">
             <xsl:value-of select="$items"/>
           </xsl:attribute>
         </xsl:element>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>
</xsl:stylesheet>

the result with Visual Studio 2005 is

<root>
   <Row item1="Label" item2="aardvark" />
   <Row item1="Label" item2="bear" />
   <Row item1="Label" item2="echidna" />
   <Row item1="Label" item2="kangaroo" />
   <Row item1="Label" item2="orangutang" />
   <Row item1="Label" item2="zebra" />
</root>

which is sorted properly.




-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent