2011-08-08 162 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 –