我寫XSLT文件用下面的代碼:如何避免在XSLT中生成空屬性?
<xsl:attribute name="subCode">
<xsl:value-of select="Field[@key='XY']"/>
</xsl:attribute>
比方說,我的輸入XML看起來是這樣的:
[...]
<Field key="XY"/>
[...]
在這種情況下我的XSLT會生成以下的輸出:
<SomeElement subCode="">
[...]
</SomeElement>
我的問題是:如何刪除空屬性subCode=""
?我知道使用<xsl:if>
這樣的條件指令是可能的,但是,這似乎是一個醜陋的解決方案(因爲我在XSLT中生成了數以千計的類似屬性)。
它必須在同一個XSLT中完成。我無法在其他XSLT文件中添加後期處理。
除此之外,輸出XML已定義了其XSD架構。架構說這個屬性是可選。也許有一種方法可以將XSD模式應用於輸出XML?
在此先感謝您的幫助!
好問題,+1。查看我的答案,獲得所有答案中最短,最精確的完整解決方案。 – 2011-04-21 12:52:13