2016-12-14 26 views
0

我試圖從以下XML中生成html。如何在xslt中給出篩選標記

<students> 
    <class action="update">2</class> 
    <section action="update">B</class> 
<student> 
    <name>ravi</name> 
    <skill action="update">badminton</skill> 
</student> 
<student action="insert"> 
    <name>gauri</name> 
    <skill>tennis</skill> 
</student> 
</students> 

我正在寫一個xsl文件來生成下表。

tag  subtag action 
class    update 
section    update 
ravi  skill  update 
gauri    insert 

我的xsl文件看起來像這樣。

<xsl:template match="/*"> 
    <h2>My CD Collection</h2> 
    <table> 
     <tr> 
     <th>tag</th> 
     <th>subtag</th> 
     <th>action</th> 
     </tr> 
    <xsl:for-each select="//*"> 
     <xsl:choose> 
     <xsl:when test="@Action"> 
     <tr> 
     <td><xsl:value-of select="local-name()"/></td> 
     <td></td> 
     <td><xsl:value-of select="@Action"/></td> 
     </tr> 
     </xsl:when> 
     <xsl:otherwise>  
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:for-each> 
</table> 
</xsl:template> 

但是,這是生成下表。

tag  subtag action 
class  update   
section update   
skill  update   
student insert   

如何單獨處理學生標籤。請幫忙。我是XSLT新手。

+1

,因爲你'<部分行動=「更新」>乙'。你的XSLT也沒有,因爲你有一個沒有相應的開始標籤的''。另外,你缺少''的結束標記。你能糾正這些,讓我們重新創建你的問題?謝謝! –

+0

@TimC感謝您指出這些。抱歉給你帶來不便。我希望現在的xsl可以重現問題。 –

回答

0

試試這個模板代替如不能很好地形成你的XML首發

<xsl:template match="/*"> 
<table> 
    <tr> 
     <th>tag</th> 
     <th>subtag</th> 
     <th>action</th> 
    </tr> 
    <xsl:for-each select="*"> 
     <tr> 
      <td> 
       <xsl:choose> 
       <xsl:when test="name"><xsl:value-of select="name"/></xsl:when> 
       <xsl:otherwise><xsl:value-of select="local-name()"/></xsl:otherwise> 
       </xsl:choose> 
      </td> 
      <xsl:choose> 
       <xsl:when test="@action"> 
       <td></td> 
       <td><xsl:value-of select="@action"/></td> 
       </xsl:when> 
       <xsl:otherwise> 
       <td><xsl:value-of select="local-name(*[@action])"/></td> 
       <td><xsl:value-of select="*/@action"/></td> 
       </xsl:otherwise> 
      </xsl:choose> 
     </tr> 
    </xsl:for-each> 
    </table> 
</xsl:template> 
+0

首先感謝解決方案。它工作正常。但是,我們如何處理標籤有多處更改的情況。 –

+0

很難說,因爲你沒有在你的問題中顯示這個,所以我不知道你期望在這種情況下的輸出。您可能需要提出一個新問題。謝謝! –

+0

非常感謝您的幫助。我會發佈一個新問題。 –