2010-12-08 48 views
2

我無法讓XSLT僅返回XML中的類別值。爲什麼lastupdate和路徑被返回? ...我怎樣才能阻止呢?提前致謝。XSLT顯式節點選擇無返回

XML文檔

<?xml version="1.0"?> 
<categories count="3"> 
    <lastupdate>08/12/2010 12:27</lastupdate> 
    <path>C:\</path> 
    <category>Music</category> 
    <category>News</category> 
    <category>Sport</category> 
</categories> 

我的XSLT

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="categories"> 
     <html> 
      <body> 
       <table border="0" cellpadding="0" cellspacing="0"> 
        <tbody> 
         <tr> 
          <td> 
           <xsl:apply-templates/> 
          </td> 
         </tr> 
        </tbody> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="category"> 
     <a> 
      <xsl:value-of select="." /> 
     </a> 
    </xsl:template> 
</xsl:stylesheet> 

輸出HTML

<html> 
    <body> 
     <table border="0" cellpadding="0" cellspacing="0"> 
      <tbody> 
       <tr> 
        <td>08/12/2010 12:27C:\ 
         <a>Music</a> 
         <a>News</a> 
         <a>Sport</a> 
        </td> 
       </tr> 
      </tbody> 
     </table> 
    </body> 
</html> 
+0

你的輸出表明,這是不完整的XSLT your're運行。我懷疑你的xslt中有更多東西將`lastupdate`和`path`複製到你的輸出中。 – Filburt 2010-12-08 15:20:52

+0

我同意。桌子從哪裏來?從您發佈的代碼中不清楚 – 2010-12-08 15:22:39

+0

從他樣本中的縮進來看,他將表格放在body和apply-templates之間,這也將解釋輸出。 – TToni 2010-12-08 15:25:37

回答

1

在你的代碼是爲所有categories'子元素節點應用模板。見http://www.w3.org/TR/xslt#built-in-rule

因此,你需要下面的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/> 
    <xsl:template match="categories"> 
     <html> 
      <body> 
       <xsl:apply-templates select="category"/> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="category"> 
     <a> 
      <xsl:value-of select="." /> 
     </a> 
    </xsl:template> 
</xsl:stylesheet> 

,從而獲得所需輸出繼電器:

<html> 
    <body> 
     <a>Music</a> 
     <a>News</a> 
     <a>Sport</a> 
    </body> 
</html> 
1

你< XSL:申請模板/>適用於所有匹配的模板所有子節點。

因爲您尚未爲lastupdate和路徑定義匹配模板,所以XSLT會將其應用於默認模板,在這種情況下,將複製文本內容。

如果您想禁用此選項,您必須覆蓋默認模板(通常不太好)或限制模板應用程序在您要處理的節點上。在你的榜樣擴大應用模板來

<xsl:apply-templates select="./category"/> 
0

它可能是你有這樣的事情的地方在你的XSLT的底部:

<xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
</xsl:template> 

這可以解釋爲什麼你結束了這些值在你的輸出中間的某個地方。

3

爲什麼lastupdate和路徑是 返回?

因爲built-in rules,正是這兩個:

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

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

apply-templates相同具有select="node()"。然後,lastupdatepath元素通過元素的內置規則(僅將模板應用於子節點)進行匹配,並且其文本節點子節點通過內置的文本節點規則(輸出字符串值)進行匹配。

...我該如何解決這個問題?

覆蓋內置的一個規則中,如:

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

含義沒有文本節點輸出。或者使用推式的方法類似

<xsl:template match="categories"> 
<html> 
    <body> 
    <xsl:apply-templates select="category"/> 
    </body> 
</html> 
</xsl:template>