2013-08-02 29 views
1

xsl:stylesheet我有這樣的「身份像」改造,消除意見,空(終端)標籤和空屬性...但xsl:when不工作如何通過XSLT消除所有<TAG/>和所有attribute =「」?

<xsl:template match="node()"> 
    <xsl:choose> 
    <xsl:when test="name()='p' and not(./*) and not(normalize-space(.))"></xsl:when> 
    <xsl:when test="not(name()='img') and not(name()='br') and not(./*) and not(text())" 
    ></xsl:when> <!-- this line NOT WORKS --> 
    <xsl:otherwise><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

    <xsl:template match="@*"> 
    <xsl:choose> 
    <xsl:when test="not(normalize-space(.))"></xsl:when> 
    <xsl:otherwise><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

    <xsl:template match="comment()"></xsl:template> 

Whow表達狀態空標籤在這種情況下?

PS:「空的規則」是explained here,我嘗試使用它,但不明白爲什麼不工作。

+0

唯一的參考是郵件列表?! :D rtfm!順便說一句,請接受答案 – hek2mgl

回答

1

空元素是沒有子節點的元素。

模板匹配優先級是你的朋友......以下應該是那種符合你的描述的身份樣式表加上我認爲你正在使用圖像和中斷元素。

<?xml version="1.0" encoding="US-ASCII"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       version="1.0"> 

<!--toss these--> 
<xsl:template match="comment() | 
        *[not(node())] | 
        @*[not(normalize-space())]"/> 

<!--preserve these--> 
<xsl:template match="img|br" priority="1"> 
    <xsl:call-template name="identity"/> 
</xsl:template> 

<!--preserve everything else--> 
<xsl:template match="@*|node()" name="identity"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

非常感謝,它的工作原理!這是一個更好,更清潔的解決方案!關於我的測試...當我使用''添加另一個模板時,其中函數'xslib :: chgCase'使用DOM,它也會破壞樣式表中的一些「折騰事件」......但是[註冊函數]有一些「副作用」(https://en.wikibooks.org/ wiki/PHP_Programming/XSL/registerPHPFunctions)...現在我孤立了。 –