xml - Why doesn't xsl:sort work when using xsl:key? -


given xml:

<?xml version="1.0" encoding="utf-8" ?> <data>     <chapter-section-datasource>         <section id="12" handle="chapter-section">chapter section</section>         <entry id="94">             <order handle="1">1</order>         </entry>     </chapter-section-datasource>     <page-content>         <section id="9" handle="page-content">x</section>         <chapter-section link-id="94">             <entry id="87">                 <section-id>0</section-id>             </entry>             <entry id="91">                 <section-id>2</section-id>             </entry>             <entry id="93">                 <section-id>1</section-id>             </entry>             <entry id="103">                 <section-id>3</section-id>             </entry>         </chapter-section>     </page-content> </data> 

and xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">    <xsl:template match="data">     <xsl:apply-templates select="chapter-section-datasource/entry[1]" mode="bal"/>   </xsl:template>    <!-- map results -->   <xsl:key name="guide" match="chapter-section" use="@link-id" />    <xsl:template match="entry" mode="bal">     <xsl:apply-templates select="key('guide', @id)" mode="balguide">       <xsl:sort select="section-id" data-type="number" order="ascending" />     </xsl:apply-templates>   </xsl:template>    <xsl:template match="chapter-section/entry" mode="balguide">     <xsl:element name="div">           <xsl:value-of select="section-id"/>     </xsl:element>   </xsl:template>  </xsl:stylesheet> 

the expected output is: 0,1,2,3

the actual output is: 0,2,1,3

why isn't sort working expect? note complexity of transform due more complex xml , xsl has been simplified example.

if matters, transformation done in c#

system.xml.xmldocument doc = new system.xml.xmldocument(); doc.load(server.mappath("xml/simple.xml")); system.xml.xsl.xsltransform trans = new system.xml.xsl.xsltransform(); trans.load(server.mappath("xml/simple.xsl")); xml1.document = doc; xml1.transform = trans; 

you need

  <xsl:template match="entry" mode="bal">     <xsl:apply-templates select="key('guide', @id)/entry" mode="balguide">       <xsl:sort select="section-id" data-type="number" order="descending" />     </xsl:apply-templates>   </xsl:template> 

that is, need make sure process , sort entry elements (and not sole parent no sorting happens , default template kicks in process entry elements in original order).

here full stylesheet, changed order="ascending" want 0,1,2,3:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">    <xsl:template match="data">     <xsl:apply-templates select="chapter-section-datasource/entry[1]" mode="bal"/>   </xsl:template>    <!-- map results -->   <xsl:key name="guide" match="chapter-section" use="@link-id" />    <xsl:template match="entry" mode="bal">     <xsl:apply-templates select="key('guide', @id)/entry" mode="balguide">       <xsl:sort select="section-id" data-type="number" order="ascending" />     </xsl:apply-templates>   </xsl:template>    <xsl:template match="chapter-section/entry" mode="balguide">     <div>           <xsl:value-of select="section-id"/>     </div>   </xsl:template>  </xsl:stylesheet> 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -