2011-07-29 147 views
1
I have the following xml and I need to sort this xml using XSLT based on the value of 
the "CONTENT_LENGTH" KEY ie. based on the attributes in the element <DOC_DETAIL  
KEY="CONTENT_LENGTH" VALUE="14"/> 

<?xml version="1.0" encoding="UTF-8"?> 
<DOC_LISTS> 
<DOC_LIST> 
<DOC_URL>testurl4</DOC_URL> 
<DOC_DETAILS> 
<DOC_DETAIL KEY="TITLE" VALUE="Red Dragon"/> 
<DOC_DETAIL KEY="LANGUAGE" VALUE="english"/> 
<DOC_DETAIL KEY="CONTENT_LENGTH" VALUE="14"/> 
</DOC_DETAILS> 
</DOC_LIST> 
<DOC_LIST> 
<DOC_URL>testurl2</DOC_URL> 
<DOC_DETAILS> 
<DOC_DETAIL KEY="TITLE" VALUE="Hannibal Rising"/> 
<DOC_DETAIL KEY="LANGUAGE" VALUE="english"/> 
<DOC_DETAIL KEY="CONTENT_LENGTH" VALUE="7"/> 
</DOC_DETAILS> 
</DOC_LIST> 
</DOC_LISTS> 

What will be the sort select statement in <xsl:sort select=""/>? 

回答

1

您将使用身份模板

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 
<xsl:output method="xml" indent="yes"/> 

<xsl:template match="//DOC_LISTS"> 
    <xsl:for-each select="DOC_LIST"> 
    <xsl:sort select="DOC_DETAILS/DOC_DETAIL[@KEY='CONTENT_LENGTH']/@VALUE" data-type="number"/> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:for-each> 
</xsl:template> 

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

感谢鲍勃。这已经解决了我的问题。 – askris4me

+0

非常好,你会接受答案吗? –

0

当使用身份转换要DOC_DETAILS/DOC_DETAIL[@KEY='CONTENT_LENGTH']/@VALUE

完全简单的例子实在是即时排序只是应用模板值如下:

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

    <xsl:template match="DOC_LISTS"> 
     <xsl:copy> 
     <xsl:apply-templates select="DOC_LIST"> 
      <xsl:sort select=" 
       DOC_DETAILS 
       /DOC_DETAIL[@KEY='CONTENT_LENGTH'] 
        /@VALUE" data-type="number"/> 
     </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 

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

</xsl:stylesheet>