I need to find out how to detect if a node contains significant information.
The following example shows what is not considered "significant" information by me:
<node>
<node1> </node1>
</br></br>
<node1>
<node2></br> </node2>
</br></br>
</node1>
<!--
开发者_如何学C and so on...
-->
</node>
This <node>
is "empty" for me.
Here is how to do it:
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"text()
[translate(normalize-space(), ' ','')
= ''
]"/>
</xsl:stylesheet>
When this transformation is applied to the following XML document (the one you provided was severely malformed -- non-well formed in numerous ways!!):
<!DOCTYPE node [ <!ENTITY nbsp " "> ]>
<node>
<node1> </node1>
<br></br>
<node1>
<node2><br/> </node2>
<br></br>
</node1>
<!--
and so on...
-->
</node>
then the wanted result is produced:
<node>
<node1/>
<br/>
<node1>
<node2>
<br/>
</node2>
<br/>
</node1><!--
and so on...
-->
</node>
This technique can be generalized:
You can have all whetespace-characters in an xsl:variable, then simply override the identity rule with this template:
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vwhiteSpace" select="' 	

 '"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:if test="translate(., $vwhiteSpace,'') != ''">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
And you can specify all additional characters you consider "white-space" in $vwhiteSpace
Update: The OP indicated in a comment that he actually wants to see if a "node" is significant or not -- not to "clean a node".
The solution to this is already contained in my solution to the initial problem:
<xsl:variable name="vIsSignificant" select=
"translate(., $vwhiteSpace,'') != ''"/>
精彩评论