2011-08-08 35 views
2

我試着給所有元素進行排序,然後屬性,其中香港專業教育學院得到了工作,但我不能想出爲我的生活如何刪除空屬性在XSLT刪除空屬性

這裏是那種XSLT

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

<xsl:output method="xml" indent="yes"/> 

<xsl:template match="@* | node()"> 
    <xsl:copy> 

     <xsl:apply-templates > 
      <xsl:sort select="local-name()"/> 
     </xsl:apply-templates> 

    </xsl:copy> 
</xsl:template> 


<xsl:template match="*[*]"> 

    <xsl:copy> 
     <xsl:apply-templates select="@*" > 
      <xsl:sort select="local-name()" /> 
     </xsl:apply-templates> 

     <xsl:apply-templates select="*" > 
      <xsl:sort select="local-name()"/> 
     </xsl:apply-templates> 

    </xsl:copy> 
</xsl:template> 

感謝所有幫助

+0

屬性在XML中沒有順序,所以不要指望順序始終如預期。 – Lucero

+0

沒有按照字母順序排序,也沒有對索引位置感興趣 –

+0

重點在於,根據變換的目標(例如XmlWriter實現),寫入的屬性的順序不一定總是對應於順序你正在指定。我只是讓你意識到這個事實。 – Lucero

回答

3

嗯,你處理屬性節點的唯一地方<xsl:apply-templates select="@*">如此改變,爲<xsl:apply-templates select="@*[normalize-space()]">可能就足夠了。

1
<xsl:template match="@*"> 
    <xsl:if test="string-length(.)!=0"> 
     <xsl:copy /> 
    </xsl:if> 
</xsl:template> 

<xsl:template match="node()"> <!-- replaces the "match='@* | node()'" template --> 
    <xsl:copy> 
     <xsl:apply-templates > 
      <xsl:sort select="local-name()"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 
+0

我之前做過類似的事情,但它沒有工作。但是我沒有使用'字符串長度',所以加一個:D –