2012-10-24 46 views
3

我有下面的XML代碼非匹配的子元素也出現在XSL模板

<?xml version="1.0"?> 
<?xml-stylesheet type="text/xsl" href="new_proto.xsl"?> 
<Return> 
<SDSSJ type="form" /> 
<JSFK type="form" /> 
<KJFK type="form2" /> 
<HJDHH type="form"> New Val </HJDHH> 
<NNDJB type="some"> 
    <DJSJJ type="form"> 
     THIS 
    </DJSJJ> 
    <KAKKK type="nope"> 
    DONT 
    </KAKKK> 
Not 
</NNDJB> 
</Return> 

我只是想獲得具有屬性類型=「形式」的所有節點的名稱。所以我嘗試了以下XSL。

1 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    2 <xsl:output method="html" omit-xml-declaration="yes" indent="no"/> 
    3  <xsl:template match="//*[@type='form']"> 
    4  <xsl:value-of select="name()" /> 
    5  </xsl:template> 
    6 </xsl:stylesheet> 

但不是SDSSJ JSFK HJDHH DJSJJ,我得到SDSSJ JSFK HJDHH DJSJJ DONT Not。爲什麼不遵守模板的子元素仍然出現?我應該怎麼做才能擺脫它們?

回答

5

添加這個模板:

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

例子:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" omit-xml-declaration="yes" indent="no"/> 

    <xsl:template match="//*[@type='form']"> 
     <xsl:value-of select="name()"/> 
    </xsl:template> 

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

</xsl:stylesheet> 

默認的文本被傳遞到輸出。上述模板將剝離不匹配的文本。

這裏的what the spec says有關默認XSLT模板規則:

There is a built-in template rule to allow recursive processing 
to continue in the absence of a successful pattern match by an 
explicit template rule in the stylesheet. This template rule 
applies to both element nodes and the root node. The following 
shows the equivalent of the built-in template rule: 

<xsl:template match="*|/"> 
    <xsl:apply-templates/> 
</xsl:template> 

There is also a built-in template rule for each mode, which 
allows recursive processing to continue in the same mode in 
the absence of a successful pattern match by an explicit 
template rule in the stylesheet. This template rule applies 
to both element nodes and the root node. The following 
shows the equivalent of the built-in template rule for mode m. 

<xsl:template match="*|/" mode="m"> 
    <xsl:apply-templates mode="m"/> 
</xsl:template> 

There is also a built-in template rule for text and 
attribute nodes that copies text through: 

<xsl:template match="text()|@*"> 
    <xsl:value-of select="."/> 
</xsl:template> 

The built-in template rule for processing instructions and 
comments is to do nothing. 

<xsl:template match="processing-instruction()|comment()"/> 

The built-in template rule for namespace nodes is also to 
do nothing. There is no pattern that can match a namespace 
node; so, the built-in template rule is the only template 
rule that is applied for namespace nodes. 

The built-in template rules are treated as if they were 
imported implicitly before the stylesheet and so have 
lower import precedence than all other template rules. 
Thus, the author can override a built-in template rule 
by including an explicit template rule. 
+0

我完全陌生的XSLT。你能告訴我我應該在哪裏添加這個? – Pradep

+0

@Pradep - 我添加了一個例子。 –

+0

@Pradep - 另外,你可以從'// * [@ type ='form']''中移除''''。如果需要將值分開,您可能還想在'xsl:value-of'(帶'concat()'或'xsl:text')之後加一個空格。 –