2011-01-26 47 views
0

我使用的是XSLT一塊XML,如排序:XSLT:排序像SOLR

<feed> 
    <entry> 
     <title>A To Z</title> 
    </entry> 
    <entry> 
     <title>Action</title> 
    </entry> 
</feed> 

的XSLT的樣子:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:atom="http://www.w3.org/2005/Atom" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:param name="name" select="'title'" /> 
<xsl:param name="order" select="'ascending'" /> 

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

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

<xsl:template match="atom:feed"> 
    <xsl:copy> 
    <xsl:apply-templates select="/atom:feed/*[not(self::atom:entry)]" /> 
    <xsl:apply-templates select="/atom:feed/atom:entry"> 
    <xsl:sort select="*[name() = $name]" order="{$order}" /> 
    <xsl:sort select="atom:id" data-type="number" /> 
    </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 

我預計值的出來順序如A到Z,然後是Action,但結果卻包含相反的結果。它看起來像被忽略的空白作爲排序的值。

+0

@smokedice我刪除了一些未使用的參數和名稱空間,以使XSLT更易於閱讀。評論或編輯,如果你覺得這些對於問題很重要 – phihag 2011-01-26 12:10:36

+0

@smokedice:似乎爲我工作,它是如何失敗? – Lazarus 2011-01-26 12:19:35

+0

問題是生成的第一個標題是Action,然後是A到Z. @Lazarus是否使用Xerces來應用模板? – smokedice 2011-01-26 12:23:40

回答

0

在XSLT 1.0中,排序是實現定義的,所以很可能某些實現忽略排序的空間。你正在使用哪個實現?

我建議是這樣的:

<xsl:sort select="translate(*[name() = $name],' ','_')" order="{$order}" />

可能會解決你的問題(雖然也取決於你如何使用各種XSLT執行 '_')

1

這個樣式表:

<xsl:stylesheet version="1.0" 
xmlns:atom="http://www.w3.org/2005/Atom" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:param name="name" select="'title'" /> 
    <xsl:param name="order" select="'ascending'" /> 
    <xsl:output method="xml" encoding="UTF-8" indent="yes" /> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="atom:feed"> 
     <xsl:copy> 
      <xsl:apply-templates select="*[not(self::atom:entry)]" /> 
      <xsl:apply-templates select="atom:entry"> 
       <xsl:sort select="*[local-name() = $name]" order="{$order}" /> 
       <xsl:sort select="atom:id" data-type="number" /> 
      </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

通過此輸入(與命名空間聲明):

<feed xmlns="http://www.w3.org/2005/Atom"> 
    <entry> 
     <title>A To Z</title> 
    </entry> 
    <entry> 
     <title>Action</title> 
    </entry> 
</feed> 

輸出:

<feed xmlns="http://www.w3.org/2005/Atom"> 
    <entry> 
     <title>A To Z</title> 
    </entry> 
    <entry> 
     <title>Action</title> 
    </entry> 
</feed> 

測試與MSXSL 3/4,撒克遜人,Altova的,XQSharp。 注意:只有Oracle,和Xalan按照升序排序'A到Z'之前的'操作'。